引入css和js
<link rel="stylesheet" href="/${appName}/commons/jslib/ztreeV3.5.15/css/zTreeStyle/zTreeStyle.css" type="text/css"></link> <script type="text/javascript" src="/${appName}/commons/jslib/ztreeV3.5.15/jquery.ztree.all-3.5.js"></script>
html
<ul id="treeModule" class="ztree" style="height:280px"></ul>
加载树
/*配置*/ var setting = { check: { //显示多选框,节点中checked字段控制 enable: true, autoCheckTrigger: true, chkStyle: "checkbox", chkboxType: { "Y": "ps", "N": "ps" } }, data: { key:{ name:"moduleName" //显示节点字段 }, simpleData: { enable: true, idKey: "moduleCode", //id字段 pIdKey: "parentCode", //父级id字段 rootPId: 0 //根id值 } } }; /**菜单显示*/ var roleCode_access; function accessShow(roleCode){ roleCode_access = roleCode; $('#accesswin').modal('show'); //树 $.ajax({ url:'/${appName}/manager/moduleController/getAllModules', type:'post', async:'true', cache:false, data:{roleCode:roleCode_access}, dataType:'json', success: function(data){ $.fn.zTree.init($("#treeModule"), setting, data); } }); }
获取勾选框的值
var mids =''; var treeObj = $.fn.zTree.getZTreeObj("treeModule"); var nodes = treeObj.getCheckedNodes(true); for(i=0;i<nodes.length;i++){ mids = mids+nodes[i].moduleCode+','; }
后台代码
SysModule.java
public class SysModule { private String moduleCode; private String moduleName; private String modulePath; private String parentCode; private String isLeaf; private String sortNumber; private String checked; //是否勾选 private String open = "true"; //是否展开,默认展开 }
Controller层
/** * 获取所有菜单 * @return */ @RequestMapping("getAllModules") @ResponseBody public List<SysModule> getAllModules(String roleCode){ List<SysModule> result = moduleService.getAllModules(); List<SysModule> binded = moduleService.getModulesByRoleCode(roleCode); Iterator<SysModule> iterator = result.iterator(); /*遍历所有菜单*/ while (iterator.hasNext()){ SysModule module= iterator.next(); /*遍历所有已绑定的菜单*/ Iterator<SysModule> iterator_binded = binded.iterator(); while (iterator_binded.hasNext()){ SysModule module_binded= iterator_binded.next(); if (module.getModuleCode().equals(module_binded.getModuleCode())){ module.setChecked("true"); } } } return result; }
Service层
/** * 获取所有菜单 * @return */ public List<SysModule> getAllModules(){ List<SysModule> modules = moduleMapper.getAllModules(); return modules; } /** * 根据角色code获取绑定的菜单 * @param roleCode * @return */ public List<SysModule> getModulesByRoleCode(String roleCode){ List<SysModule> modules = moduleMapper.getModulesByRoleCode(roleCode); return modules; }
Mybatis层
/** * 获取所有菜单 * @return */ List<SysModule> getAllModules(); /** * 根据角色编码获取绑定的菜单 * @param roleCode * @return */ public List<SysModule> getModulesByRoleCode(String roleCode); <!--获取所有菜单--> <select id="getAllModules" resultType="com.autumn.pojo.SysModule"> select * from sys_module </select> <!--根据rolecode获取绑定的菜单--> <select id="getModulesByRoleCode" resultType="com.autumn.pojo.SysModule"> select sm.* from sys_module sm left join sys_role_module srm on sm.moduleCode = srm.moduleCode where srm.roleCode= #{roleCode} </select>
数据库
CREATE TABLE IF NOT EXISTS `sys_module` ( `moduleCode` varchar(36) NOT NULL COMMENT '模块编号', `moduleName` varchar(50) DEFAULT NULL COMMENT '模块名称', `modulePath` varchar(1000) DEFAULT NULL COMMENT '模块访问路径', `parentCode` varchar(36) DEFAULT NULL COMMENT '父模块编号', `isLeaf` int(11) DEFAULT NULL COMMENT '是否叶子节点:0 否,1 是', `sortNumber` int(11) DEFAULT NULL COMMENT '排序号', PRIMARY KEY (`moduleCode`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模块表'; CREATE TABLE IF NOT EXISTS `sys_role` ( `roleCode` varchar(36) NOT NULL COMMENT '角色编号', `roleName` varchar(50) NOT NULL COMMENT '角色名称', PRIMARY KEY (`roleCode`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色表'; CREATE TABLE IF NOT EXISTS `sys_role_module` ( `rmId` varchar(36) NOT NULL COMMENT '唯一编号', `roleCode` varchar(36) NOT NULL COMMENT '角色编号', `moduleCode` varchar(50) NOT NULL COMMENT '模块编号', PRIMARY KEY (`rmId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户角色-模块关联表';