zTreeUtils树状工具类

public class TreeUtil {


    /**
     * pc菜单 - 递归得到子节点
     *
     * @param pid 默认传递0
     * @param pcMenuList 角色集合
     */
    public static List getPcMehuNodeTree(String pid, List pcMenuList) {
        List newTreeDataList = new ArrayList();
        for (Menu pcMenu : pcMenuList) {
            //子节点
            if (pcMenu.getParentid().equalsIgnoreCase(pid)) {
                //递归获取子节点下的子节点
                pcMenu.setChildren(getPcMehuNodeTree(pcMenu.getId(), pcMenuList));
                newTreeDataList.add(pcMenu);
            }
        }
        return newTreeDataList;
    }

    /**
     * pc菜单 - 格式化树结构
     */
    public static List> formatPcMenuTree(List list) {
        List> resultList = new ArrayList>();
        for (Menu pcMenu : list) {
            Map result = new HashMap();
            result.put("id", pcMenu.getId());
            result.put("name", pcMenu.getName());
            result.put("exeaddress", pcMenu.getExeaddress());
            result.put("pId", pcMenu.getParentid());
            result.put("code", pcMenu.getCode());
            result.put("userid", pcMenu.getUserid());
            result.put("number", pcMenu.getNumber());
            result.put("sort", pcMenu.getSort());
            result.put("open", "true");
            if (pcMenu.getChildren().size() > 0) {
                result.put("children", formatPcMenuTree(pcMenu.getChildren()));
            }
            resultList.add(result);
        }
        return resultList;
    }

    /**
     * 菜单 - 格式化树结构
     */
    public static List> formatMenuTree(List list, List rolelist) {
        List> resultList = new ArrayList>();
        for (Menu pcMenu : list) {
            Map result = new HashMap();
            for (Menu menu : rolelist) {
                if (menu.getId().equals(pcMenu.getId())) {
                    result.put("checked", "true");
                }
                List mList = menu.getChildren();
                for (Menu m : mList) {
                    if (m.getId().equals(pcMenu.getId())) {
                        result.put("checked", "true");
                    }
                }

            }
            result.put("id", pcMenu.getId());
            result.put("name", pcMenu.getName());
            result.put("exeaddress", pcMenu.getExeaddress());
            result.put("pId", pcMenu.getParentid());
            result.put("code", pcMenu.getCode());
            result.put("userid", pcMenu.getUserid());
            result.put("number", pcMenu.getNumber());
            result.put("sort", pcMenu.getSort());
            result.put("open", "true");
            if (pcMenu.getChildren().size() > 0) {
                result.put("children", formatMenuTree(pcMenu.getChildren(), rolelist));
            }
            resultList.add(result);
        }
        return resultList;
    }

    /**
     * pc菜单 - 封装根节点
     */
    public static Map rootPcMenuTree(List> list) throws UnsupportedEncodingException {
//        String  str = URLEncoder.encode("全部菜单", "UTF-8");//URLDecoder.decode(str, "UTF-8")
        return packagingRootTree(list, "全部菜单");
    }

    /**
     * 实际执行封装根节点的方法
     */
    private static Map packagingRootTree(List> list, String name) {
        Map tree = new HashMap();
        tree.put("id", "0");
        tree.put("pid", "null");
        tree.put("name", name);
        tree.put("code", "0");
        tree.put("number", "0");
        tree.put("open", "true");
        if (list != null && list.size() > 0) {
            tree.put("children", list);
            list.forEach(maps -> {
                if (maps.get("checked") != null && maps.get("checked").equals("true")) {
                    tree.put("checked", "true");
                }
            });
        }
        return tree;
    }

}

你可能感兴趣的:(zTree)