EasyUI之Tree樹形結構(一)

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

前端代碼:前端

$("#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後臺核心代碼:bash

public List<SysMenuEntity> queryToTreeList(){
    List<SysMenuEntity> menuList = sysMenuMapper.queryList();
    if (menuList != null && menuList.size() > 0) {
        ArrayList child = new ArrayList<SysMenuEntity>();
        child.addAll(menuList);
        Iterator<SysMenuEntity> dept = menuList.iterator();
        while (dept.hasNext()){
            SysMenuEntity resource = dept.next();
            List<SysDeptEntity> 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<SysMenuEntity> menu = menuList.iterator();
        while (menu.hasNext()){
            SysMenuEntity resource = menu.next();
           if(resource.getParentId()!=0){
               menu.remove();
            }
        }
    }
    return menuList;
}
List<SysMenuEntity> getChildList(Long parentId,List<SysMenuEntity> deptList){
    List<SysMenuEntity> childList =new ArrayList<>();
    if (deptList != null && deptList.size() > 0) {
        for (SysMenuEntity resource : deptList) {
            if(parentId.equals(resource.getParentId())){
                childList.add(resource);
            }
        }
    }
    return childList;
}
複製代碼

相關文章
相關標籤/搜索