EasyUI之Tree樹形結構(二)

前端html代碼

<td>
    <select id="parentResource" class="easyui-combobox com_input" name="parentId" 
    data-options="textField:'text',valueField:'pid',required:true" style="width:173px;">
    </select>
</td>
複製代碼

前端js代碼

//增
add: function () {
    $('#SysResourceEdit').dialog({
            href: SysResource.URL.inputUI(),
            onLoad: function () {
                $('#parentResource').combotree({
                    onSelect: function (record) {
                    },
                    onChange: function (a,b) {
                    },
                    url: SysResource.URL.tree(),
                    method: 'get',
                    value:'0',
                    panelHeight: 'auto'
                });
            }
        })
        .dialog("open");
}
複製代碼

後臺Java核心代碼

@RequestMapping("/tree")
public List<Tree> tree(){
	List<SysMenuEntity> menuList = sysMenuService.queryList();
	List<Tree> trees = ResourceUtil.prepareMenuToTree(menuList);
	Tree tree = new Tree();
	tree.setText("一級目錄");
	tree.setId(0L);
	tree.setPid(-1L);
	tree.setIsLeaf(0);
	tree.setChildren(new ArrayList<>());
	trees.add(0,tree);
	return trees;
}
	
public static List<Tree> prepareMenuToTree(List<SysMenuEntity> resourceList) {
        if(resourceList == null || resourceList.size() == 0){
            return new ArrayList<>();
        }
        //目錄集合
        List<Tree> catalogMenuList = new ArrayList<>();
        //菜單集合
        List<Tree> menuList = new ArrayList<>();
        //按鈕集合
        List<Tree> buttonList = new ArrayList<>();
        resourceList.forEach(sysMenuEntity->{
            Tree menuTree = menuToTree(sysMenuEntity);
            //類型
            Integer type = menuTree.getIsLeaf();
            //目錄
            if(type == Constant.MenuType.CATALOG.getValue() ){
                catalogMenuList.add(menuTree);
            }
            //菜單
            if(type == Constant.MenuType.MENU.getValue()){
                menuList.add(menuTree);
            }
            //按鈕
            if(type == Constant.MenuType.BUTTON.getValue() ){
                buttonList.add(menuTree);
            }
        });
        catalogMenuList.forEach(catalogMenu->{
            long catalogId = catalogMenu.getId();
            //目錄的孩子
            List<Tree> catalogChildren  = catalogMenu.getChildren();
            for(Iterator<Tree> second = menuList.iterator(); second.hasNext();){
               Tree menu = second.next();
                long menuId = menu.getId();
                //菜單的孩子
                List<Tree> menuChildren  = menu.getChildren();
                if(catalogId == menu.getPid()){
                    catalogChildren.add(menu);
                    for(Iterator<Tree> buttonTree = buttonList.iterator(); buttonTree.hasNext();){
                        Tree button = buttonTree.next();
                        long pid = button.getPid();
                        if(menuId == pid){
                            menuChildren.add(button);
                            buttonTree.remove();
                        }
                    }
                    second.remove();
                }
                if(menuChildren!=null && menuChildren.size() != 0){
                    menu.setState("closed");
                }
            }
            if(catalogChildren!=null && catalogChildren.size() != 0){
                catalogMenu.setState("closed");
            }
        });
        menuList.clear();
        buttonList.clear();
        return catalogMenuList;
}
    
private static Tree menuToTree(SysMenuEntity resource) {
        Tree tree = new Tree();
        tree.setId(resource.getMenuId());
        tree.setText(resource.getName());
        tree.setIconCls(resource.getIcon());
        tree.setIsLeaf(resource.getType());
        tree.setPid(resource.getParentId());
        /*tree.setAttributes(resource);*/
        return tree;
}

public class Tree implements Serializable {
    /*節點的 id*/
    private Long id;
    /*要顯示的節點文本*/
    private String text;
    private int seq;
    /*節點狀態,'open''closed',默認是 'open'*/
    private String state = "open";// open,closed
    /*指示節點是否被選中*/
    private boolean checked = false;

    /*定義了一些子節點的節點數組*/
    private List<Tree> children = new ArrayList<>();
    /*用來顯示圖標的 css class*/
    private String iconCls;

    private Long pid;

    /*把指定的節點定義成葉節點*/
    private Integer isLeaf;
}
複製代碼

最終效果:

相關文章
相關標籤/搜索