Commit 2f5298bc by fsn

菜单添加和修改更加人性化,不需手动输入父级编号和层级

parent 4eec946f
...@@ -160,6 +160,24 @@ public class ConstantFactory { ...@@ -160,6 +160,24 @@ public class ConstantFactory {
} }
/** /**
* 获取菜单名称通过编号
*/
public String getMenuNameByCode(String code) {
if (ToolUtil.isEmpty(code)) {
return "";
} else {
Menu param = new Menu();
param.setCode(code);
Menu menu = menuMapper.selectOne(param);
if (menu == null) {
return "";
} else {
return menu.getName();
}
}
}
/**
* 获取字典名称 * 获取字典名称
*/ */
public String getDictName(Integer dictId){ public String getDictName(Integer dictId){
......
...@@ -11,13 +11,14 @@ import com.stylefeng.guns.common.controller.BaseController; ...@@ -11,13 +11,14 @@ import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum; import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException; import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.node.ZTreeNode; import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.common.persistence.dao.MenuMapper;
import com.stylefeng.guns.common.persistence.model.Menu;
import com.stylefeng.guns.core.log.LogObjectHolder; import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.MenuDao; import com.stylefeng.guns.modular.system.dao.MenuDao;
import com.stylefeng.guns.modular.system.service.IMenuService; import com.stylefeng.guns.modular.system.service.IMenuService;
import com.stylefeng.guns.modular.system.warpper.MenuWarpper; import com.stylefeng.guns.modular.system.warpper.MenuWarpper;
import com.stylefeng.guns.common.persistence.dao.MenuMapper;
import com.stylefeng.guns.common.persistence.model.Menu;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
...@@ -77,7 +78,9 @@ public class MenuController extends BaseController { ...@@ -77,7 +78,9 @@ public class MenuController extends BaseController {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL); throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
} }
Menu menu = this.menuMapper.selectById(menuId); Menu menu = this.menuMapper.selectById(menuId);
model.addAttribute(menu); Map<String, Object> menuMap = BeanKit.beanToMap(menu);
menuMap.put("pcodeName",ConstantFactory.me().getMenuNameByCode(menu.getPcode()));
model.addAttribute("menu", menuMap);
LogObjectHolder.me().set(menu); LogObjectHolder.me().set(menu);
return PREFIX + "menu_edit.html"; return PREFIX + "menu_edit.html";
} }
...@@ -93,6 +96,9 @@ public class MenuController extends BaseController { ...@@ -93,6 +96,9 @@ public class MenuController extends BaseController {
if (result.hasErrors()) { if (result.hasErrors()) {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL); throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
} }
//设置父级菜单编号
menuSetPcode(menu);
this.menuMapper.updateById(menu); this.menuMapper.updateById(menu);
return SUCCESS_TIP; return SUCCESS_TIP;
} }
...@@ -118,6 +124,9 @@ public class MenuController extends BaseController { ...@@ -118,6 +124,9 @@ public class MenuController extends BaseController {
if (result.hasErrors()) { if (result.hasErrors()) {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL); throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
} }
//设置父级菜单编号
menuSetPcode(menu);
menu.setStatus(MenuStatus.ENABLE.getCode()); menu.setStatus(MenuStatus.ENABLE.getCode());
this.menuMapper.insert(menu); this.menuMapper.insert(menu);
return SUCCESS_TIP; return SUCCESS_TIP;
...@@ -156,7 +165,7 @@ public class MenuController extends BaseController { ...@@ -156,7 +165,7 @@ public class MenuController extends BaseController {
} }
/** /**
* 获取菜单列表 * 获取菜单列表(首页用)
*/ */
@RequestMapping(value = "/menuTreeList") @RequestMapping(value = "/menuTreeList")
@ResponseBody @ResponseBody
...@@ -166,6 +175,17 @@ public class MenuController extends BaseController { ...@@ -166,6 +175,17 @@ public class MenuController extends BaseController {
} }
/** /**
* 获取菜单列表(选择父级菜单用)
*/
@RequestMapping(value = "/selectMenuTreeList")
@ResponseBody
public List<ZTreeNode> selectMenuTreeList() {
List<ZTreeNode> roleTreeList = this.menuDao.menuTreeList();
roleTreeList.add(ZTreeNode.createParent());
return roleTreeList;
}
/**
* 获取角色列表 * 获取角色列表
*/ */
@RequestMapping(value = "/menuTreeListByRoleId/{roleId}") @RequestMapping(value = "/menuTreeListByRoleId/{roleId}")
...@@ -181,4 +201,20 @@ public class MenuController extends BaseController { ...@@ -181,4 +201,20 @@ public class MenuController extends BaseController {
} }
} }
/**
* 根据请求的父级菜单编号设置pcode和层级
*/
private void menuSetPcode(@Valid Menu menu) {
if(ToolUtil.isEmpty(menu.getPcode()) || menu.getPcode().equals("0")){
menu.setPcode("0");
menu.setLevels(1);
}else{
int code = Integer.parseInt(menu.getPcode());
Menu pMenu = menuMapper.selectById(code);
Integer pLevels = pMenu.getLevels();
menu.setPcode(pMenu.getCode());
menu.setLevels(pLevels + 1);
}
}
} }
...@@ -9,13 +9,16 @@ ...@@ -9,13 +9,16 @@
<div class="col-sm-6 b-r"> <div class="col-sm-6 b-r">
<#input id="name" name="名称" underline="true" /> <#input id="name" name="名称" underline="true" />
<#input id="code" name="菜单编号" underline="true" /> <#input id="code" name="菜单编号" underline="true" />
<#input id="pcode" name="父级编号" underline="true" /> <#input id="pcodeName" name="父级编号" underline="true"
<#input id="icon" name="图标" underline="false" /> hidden="pcode" readonly="readonly"
clickFun="MenuInfoDlg.showMenuSelectTree(); return false;"
style="background-color: #ffffff !important;"
selectFlag="true" selectId="pcodeTreeDiv" selectTreeId="pcodeTree" selectStyle="width:244px !important;"/>
</div> </div>
<div class="col-sm-6"> <div class="col-sm-6">
<#input id="url" name="请求地址" underline="true" /> <#input id="url" name="请求地址" underline="true" />
<#input id="num" name="排序" underline="true" /> <#input id="num" name="排序" underline="true" />
<#input id="levels" name="层级" underline="false" /> <#input id="icon" name="图标" underline="false" />
</div> </div>
</div> </div>
......
@layout("/common/_container.html"){ @layout("/common/_container.html"){
<div class="ibox float-e-margins"> <div class="ibox float-e-margins">
<div class="ibox-content"> <div class="ibox-content">
<div class="form-horizontal"> <div class="form-horizontal">
<input type="hidden" id="id" value="${menu.id}">
<div class="row"> <input type="hidden" id="id" value="${menu.id}">
<div class="col-sm-6 b-r">
<#input id="name" name="名称" value="${menu.name}" underline="true" />
<#input id="code" name="菜单编号" value="${menu.code}" underline="true" />
<#input id="pcode" name="父级编号" value="${menu.pcode}" underline="true" />
<#input id="icon" name="图标" underline="false" value="${menu.icon}"/>
</div>
<div class="col-sm-6">
<#input id="url" name="请求地址" value="${menu.url}" underline="true" />
<#input id="num" name="排序" value="${menu.num}" underline="true" />
<#input id="levels" name="层级" value="${menu.levels}" underline="false" />
</div>
</div>
<div class="row btn-group-m-t"> <div class="row">
<div class="col-sm-10"> <div class="col-sm-6 b-r">
<#button btnCss="info" name="提交" id="ensure" icon="fa-check" clickFun="MenuInfoDlg.editSubmit()"/> <#input id="name" name="名称" value="${menu.name}" underline="true" />
<#button btnCss="danger" name="取消" id="cancel" icon="fa-eraser" clickFun="MenuInfoDlg.close()"/> <#input id="code" name="菜单编号" value="${menu.code}" underline="true" />
</div> <#input id="pcodeName" name="父级编号" value="${menu.pcodeName}" underline="true"
</div> hidden="pcode" readonly="readonly" hiddenValue="${menu.pcode}"
</div> clickFun="MenuInfoDlg.showMenuSelectTree(); return false;"
style="background-color: #ffffff !important;"
selectFlag="true" selectId="pcodeTreeDiv" selectTreeId="pcodeTree" selectStyle="width:244px !important;"/>
</div>
<div class="col-sm-6">
<#input id="url" name="请求地址" value="${menu.url}" underline="true" />
<#input id="num" name="排序" value="${menu.num}" underline="true" />
<#input id="icon" name="图标" underline="false" value="${menu.icon}"/>
</div>
</div>
</div> <div class="row btn-group-m-t">
<div class="col-sm-10">
<#button btnCss="info" name="提交" id="ensure" icon="fa-check" clickFun="MenuInfoDlg.editSubmit()"/>
<#button btnCss="danger" name="取消" id="cancel" icon="fa-eraser" clickFun="MenuInfoDlg.close()"/>
</div>
</div>
</div>
</div>
</div> </div>
<script src="${ctxPath}/static/modular/system/menu/menu_info.js"></script> <script src="${ctxPath}/static/modular/system/menu/menu_info.js"></script>
@} @}
...@@ -64,7 +64,7 @@ var Feng = { ...@@ -64,7 +64,7 @@ var Feng = {
closeBtn: 0 closeBtn: 0
}); });
}, },
showInputTree: function (inputId, inputTreeContentId) { showInputTree: function (inputId, inputTreeContentId, leftOffset, rightOffset) {
var onBodyDown = function (event) { var onBodyDown = function (event) {
if (!(event.target.id == "menuBtn" || event.target.id == inputTreeContentId || $(event.target).parents("#" + inputTreeContentId).length > 0)) { if (!(event.target.id == "menuBtn" || event.target.id == inputTreeContentId || $(event.target).parents("#" + inputTreeContentId).length > 0)) {
$("#" + inputTreeContentId).fadeOut("fast"); $("#" + inputTreeContentId).fadeOut("fast");
...@@ -72,13 +72,19 @@ var Feng = { ...@@ -72,13 +72,19 @@ var Feng = {
} }
}; };
var inputDiv = $("#" + inputId); if(leftOffset == undefined && rightOffset == undefined){
var inputDivOffset = $("#" + inputId).offset(); var inputDiv = $("#" + inputId);
var inputDivOffset = $("#" + inputId).offset();
$("#" + inputTreeContentId).css({ $("#" + inputTreeContentId).css({
left: inputDivOffset.left + "px", left: inputDivOffset.left + "px",
top: inputDivOffset.top + inputDiv.outerHeight() + "px" top: inputDivOffset.top + inputDiv.outerHeight() + "px"
}).slideDown("fast"); }).slideDown("fast");
}else{
$("#" + inputTreeContentId).css({
left: leftOffset + "px",
top: rightOffset + "px"
}).slideDown("fast");
}
$("body").bind("mousedown", onBodyDown); $("body").bind("mousedown", onBodyDown);
}, },
......
...@@ -2,91 +2,111 @@ ...@@ -2,91 +2,111 @@
* 菜单详情对话框 * 菜单详情对话框
*/ */
var MenuInfoDlg = { var MenuInfoDlg = {
menuInfoData : {} menuInfoData: {},
ztreeInstance: null
}; };
/** /**
* 清除数据 * 清除数据
*/ */
MenuInfoDlg.clearData = function() { MenuInfoDlg.clearData = function () {
this.roleInfoData = {}; this.roleInfoData = {};
} }
/** /**
* 设置对话框中的数据 * 设置对话框中的数据
* *
* @param key 数据的名称 * @param key 数据的名称
* @param val 数据的具体值 * @param val 数据的具体值
*/ */
MenuInfoDlg.set = function(key, val) { MenuInfoDlg.set = function (key, val) {
this.roleInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value; this.roleInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
return this; return this;
} }
/** /**
* 设置对话框中的数据 * 设置对话框中的数据
* *
* @param key 数据的名称 * @param key 数据的名称
* @param val 数据的具体值 * @param val 数据的具体值
*/ */
MenuInfoDlg.get = function(key) { MenuInfoDlg.get = function (key) {
return $("#" + key).val(); return $("#" + key).val();
} }
/** /**
* 关闭此对话框 * 关闭此对话框
*/ */
MenuInfoDlg.close = function() { MenuInfoDlg.close = function () {
parent.layer.close(window.parent.Menu.layerIndex); parent.layer.close(window.parent.Menu.layerIndex);
} }
/** /**
* 收集数据 * 收集数据
*/ */
MenuInfoDlg.collectData = function() { MenuInfoDlg.collectData = function () {
this.set('id').set('name').set('code').set('pcode').set('url').set('num').set('levels').set('icon'); this.set('id').set('name').set('code').set('pcode').set('url').set('num').set('levels').set('icon');
} }
/** /**
* 提交添加用户 * 提交添加用户
*/ */
MenuInfoDlg.addSubmit = function() { MenuInfoDlg.addSubmit = function () {
this.clearData(); this.clearData();
this.collectData(); this.collectData();
//提交信息 //提交信息
var ajax = new $ax(Feng.ctxPath + "/menu/add", function(data){ var ajax = new $ax(Feng.ctxPath + "/menu/add", function (data) {
Feng.success("添加成功!"); Feng.success("添加成功!");
window.parent.Menu.table.refresh(); window.parent.Menu.table.refresh();
MenuInfoDlg.close(); MenuInfoDlg.close();
},function(data){ }, function (data) {
Feng.error("添加失败!" + data.responseJSON.message + "!"); Feng.error("添加失败!" + data.responseJSON.message + "!");
}); });
ajax.set(this.roleInfoData); ajax.set(this.roleInfoData);
ajax.start(); ajax.start();
} }
/** /**
* 提交修改 * 提交修改
*/ */
MenuInfoDlg.editSubmit = function() { MenuInfoDlg.editSubmit = function () {
this.clearData(); this.clearData();
this.collectData(); this.collectData();
//提交信息 //提交信息
var ajax = new $ax(Feng.ctxPath + "/menu/edit", function(data){ var ajax = new $ax(Feng.ctxPath + "/menu/edit", function (data) {
Feng.success("修改成功!"); Feng.success("修改成功!");
window.parent.Menu.table.refresh(); window.parent.Menu.table.refresh();
MenuInfoDlg.close(); MenuInfoDlg.close();
},function(data){ }, function (data) {
Feng.error("修改失败!" + data.responseJSON.message + "!"); Feng.error("修改失败!" + data.responseJSON.message + "!");
}); });
ajax.set(this.roleInfoData); ajax.set(this.roleInfoData);
ajax.start(); ajax.start();
} }
$(function() { /**
* 点击父级编号input框时
*/
MenuInfoDlg.onClickDept = function (e, treeId, treeNode) {
$("#pcodeName").attr("value", MenuInfoDlg.ztreeInstance.getSelectedVal());
$("#pcode").attr("value", treeNode.id);
};
/**
* 显示父级菜单选择的树
*/
MenuInfoDlg.showMenuSelectTree = function () {
Feng.showInputTree("pcodeName", "pcodeTreeDiv", 15, 34);
};
$(function () {
var ztree = new $ZTree("pcodeTree", "/menu/selectMenuTreeList");
ztree.bindOnClick(MenuInfoDlg.onClickDept);
ztree.init();
MenuInfoDlg.ztreeInstance = ztree;
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment