EasyUI之Tree树形结构(一)

官网地址:www.jeasyui.net/plugins/186…

前端代码:

$("#menuTreegrid").treegrid({
        url: SysResource.URL.list(),
        method: 'get',
        pagination: false,
        pageSize: 30,
        width:'100%',
        toolbar: '#SysResourceToolbar',
        idField: 'menuId',
        treeField: 'name',
        parentField: 'parentId', //父级菜单
        singleSelect: false,
        collapsible: false, // panel是否折叠
        rownumbers: true,
        nowrap: false, // 允许换行
        autoRowHeight: false, //自动行高
        autoRowWidth: true,
        animate:true,
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'menuId', title: '主键', hidden: true},
            {field: 'name', title: '菜单名称', width: '10%', hidden: false},
            {field: 'url', title: '菜单路径', width: '10%', hidden: false },
            {field: 'description', title: '菜单描述', width: '7.917%', hidden: false},
            {field: 'perms', title: '授权标识', width: '20%', hidden: false},
            {field: 'orderNum', title: '排序', width: '3%', hidden: false},
            {
                field: 'delFlag',
                title: '状态',
                width: '3%',
                hidden: false,
                formatter: function (value, row, index) {
                    return value == 0 ? '启用' : '停用';
                }
            },
            {
                field: 'type',
                title: '资源类别',
                width: '5%',
                hidden: false,
                formatter: function (value, row, index) {
                    return value == 0 ? '目录' : value == 1 ? '菜单' : '按钮';
                }
            },
        ]],
        //对返回的数据进行处理,便于显示树形结构
        loadFilter: function (resultData) {
            return resultData.data;
        },
        //设置选中事件,清除之前的行选择
        onClickRow: function (row) {
            $("#menuTreegrid").treegrid("unselectAll");
            $("#menuTreegrid").treegrid("selectRow",row.menuId);
        },
});
复制代码

数据库结构:

CREATE TABLE `sys_menu` (
  `menu_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `parent_id` bigint(20) DEFAULT NULL COMMENT '父菜单ID,一级菜单为0',
  `name` varchar(50) DEFAULT NULL COMMENT '菜单名称',
  `description` varchar(255) DEFAULT NULL COMMENT '描述',
  `url` varchar(200) DEFAULT NULL COMMENT '菜单URL',
  `perms` varchar(500) DEFAULT NULL COMMENT '授权(多个用逗号分隔,如:user:list,user:create)',
  `type` int(11) DEFAULT NULL COMMENT '类型   0:目录   1:菜单   2:按钮',
  `icon` varchar(50) DEFAULT NULL COMMENT '菜单图标',
  `order_num` int(11) DEFAULT NULL COMMENT '排序',
  `create_by` varchar(64) DEFAULT NULL COMMENT '创建人',
  `create_time` bigint(50) DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) DEFAULT NULL COMMENT '修改人',
  `update_time` bigint(50) DEFAULT NULL COMMENT '修改时间',
  `del_flag` tinyint(4) DEFAULT '0' COMMENT '是否删除  1:删除、 0:正常',
  PRIMARY KEY (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8 COMMENT='菜单管理';
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('1', '0', '系统管理', '系统', NULL, NULL, '0', 'fa fa-cog', '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('2', '1', '员工管理', '管理员', '/user', NULL, '1', 'fa fa-user', '1', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('3', '1', '角色管理', '角色', '/role', NULL, '1', 'fa fa-user-secret', '2', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('4', '1', '菜单管理', '菜单', '/menu', NULL, '1', 'fa fa-th-list', '3', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('15', '2', '查看', NULL, NULL, 'sys:user:list,sys:user:info', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('16', '2', '新增', NULL, NULL, 'sys:user:save,sys:role:select', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('17', '2', '修改', NULL, NULL, 'sys:user:update,sys:role:select', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('18', '2', '删除', NULL, NULL, 'sys:user:delete', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('19', '3', '查看', NULL, NULL, 'sys:role:list,sys:role:info', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('20', '3', '新增', NULL, NULL, 'sys:role:save,sys:menu:perms', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('21', '3', '修改', NULL, NULL, 'sys:role:update,sys:menu:perms', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('22', '3', '删除', NULL, NULL, 'sys:role:delete', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('23', '4', '查看', NULL, NULL, 'sys:menu:list,sys:menu:info', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('24', '4', '新增', NULL, NULL, 'sys:menu:save,sys:menu:select', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('25', '4', '修改', NULL, NULL, 'sys:menu:update,sys:menu:select', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('26', '4', '删除', NULL, NULL, 'sys:menu:delete', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('31', '1', '部门管理', '部门', '/dept', NULL, '1', 'fa fa-file-code-o', '1', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('32', '31', '查看', NULL, NULL, 'sys:dept:list,sys:dept:info', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('33', '31', '新增', NULL, NULL, 'sys:dept:save,sys:dept:select', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('34', '31', '修改', NULL, NULL, 'sys:dept:update,sys:dept:select', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('35', '31', '删除', NULL, NULL, 'sys:dept:delete', '2', NULL, '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('41', '0', '商品管理', '商品', NULL, NULL, '0', 'fa fa-cog', '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('42', '41', '商品1', NULL, 'modules/sys/user.html', NULL, '1', 'fa fa-user', '1', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('43', '41', '商品2', NULL, 'modules/sys/user.html', NULL, '1', 'fa fa-user', '1', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('44', '41', '商品3', NULL, 'modules/sys/user.html', NULL, '1', 'fa fa-user', '1', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('45', '0', '订单管理', '交易', NULL, NULL, '0', 'fa fa-cog', '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('46', '0', '结算管理', '结算', NULL, NULL, '0', 'fa fa-cog', '0', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('47', '45', '订单列表', NULL, 'modules/sys/user.html', NULL, '1', 'fa fa-user', '1', NULL, NULL, NULL, NULL, '0');
INSERT INTO `localdb`.`sys_menu` (`menu_id`, `parent_id`, `name`, `description`, `url`, `perms`, `type`, `icon`, `order_num`, `create_by`, `create_time`, `update_by`, `update_time`, `del_flag`) VALUES ('48', '46', '结算管理', NULL, 'modules/sys/user.html', NULL, '1', 'fa fa-user', '1', NULL, NULL, NULL, NULL, '0');

复制代码

Java后台核心代码:

public List queryToTreeList(){
    List menuList = sysMenuMapper.queryList();
    if (menuList != null && menuList.size() > 0) {
        ArrayList child = new ArrayList();
        child.addAll(menuList);
        Iterator dept = menuList.iterator();
        while (dept.hasNext()){
            SysMenuEntity resource = dept.next();
            List childList = this.getChildList(resource.getMenuId(),child);
            if(resource.getParentId()!=0 && (childList == null || childList.size()==0) ){
                dept.remove();
            }else{
                resource.setChildren(childList);
            }
        }
    }
    if (menuList != null && menuList.size() > 0) {
        Iterator menu = menuList.iterator();
        while (menu.hasNext()){
            SysMenuEntity resource = menu.next();
           if(resource.getParentId()!=0){
               menu.remove();
            }
        }
    }
    return menuList;
}
List getChildList(Long parentId,List deptList){
    List childList =new ArrayList<>();
    if (deptList != null && deptList.size() > 0) {
        for (SysMenuEntity resource : deptList) {
            if(parentId.equals(resource.getParentId())){
                childList.add(resource);
            }
        }
    }
    return childList;
}
复制代码

转载于:https://juejin.im/post/5cea0c3de51d4556d86c7a38

你可能感兴趣的:(EasyUI之Tree树形结构(一))