java 目录、菜单递归

效果图:

json格式如下:

[{
    "level": 1,
    "dbid": 1,
    "name": "主页",
    "type": "icon-th",
    "child": [{
        "parentdbid": 1,
        "level": 2,
        "dbid": 6,
        "name": "主机管理2级-1",
        "type": "icon-minus-sign",
        "child": [{
            "parentdbid": 6,
            "level": 3,
            "dbid": 7,
            "name": "主机管理3级-1",
            "type": "icon-minus-sign",
            "child": []
        }, {
            "parentdbid": 6,
            "level": 3,
            "dbid": 8,
            "name": "主机管理3级-2",
            "type": "icon-minus-sign",
            "child": []
        }]
    }, {
        "parentdbid": 1,
        "level": 2,
        "dbid": 5,
        "name": "主机管理2级-2",
        "type": "icon-minus-sign",
        "child": []
    }, {
        "parentdbid": 1,
        "level": 2,
        "dbid": 2,
        "name": "主机管理2级-3",
        "type": "icon-minus-sign",
        "child": []
    }]
}, {
    "level": 1,
    "dbid": 3,
    "name": "信息管理",
    "type": "icon-th",
    "child": [{
        "parentdbid": 3,
        "level": 2,
        "dbid": 11,
        "name": "信息管理2.2",
        "type": "icon-minus-sign",
        "child": []
    }]
}]

数据表设计如下:

  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `parentid` int(11) NOT NULL DEFAULT '0' COMMENT '父id',
  `name` varchar(100) NOT NULL DEFAULT '' COMMENT '名称',
  `COMMENT` varchar(100) NOT NULL DEFAULT '' COMMENT '备注',
  `level` int(11) DEFAULT '1' COMMENT '级别',
  `create_time` timestamp NULL DEFAULT NULL COMMENT '创建时间',
  `create_id` int(11) DEFAULT '0' COMMENT '创建者',
  `status` int(11) DEFAULT '1' COMMENT '状态,停用,1,启用',
  `sort` int(11) DEFAULT '1' COMMENT '排序',

java代码如下:

        // 处理成目录,json格式,最后的结果集
        JSONArray jsonRes = new JSONArray();
        // 先找到所有的一级菜单
        for (ConfFlow flow : list){
            if (flow.getInt("parentid") != null && flow.getInt("parentid") == 0){
                JSONObject object = new JSONObject();
                object.put("name", flow.get("name",""));
                object.put("dbid", flow.getInt("id"));
                object.put("level", flow.getInt("level"));
                object.put("type", "icon-th");
                object.put("child", new JSONArray());
                jsonRes.add(object);
            }
        }
        // 为一级菜单设置子菜单,getChild是递归调用的
        for (Object object : jsonRes) {
            JSONObject jsonObject = (JSONObject) object;
            jsonObject.put("child", getChild(jsonObject.getInteger("dbid"), list));
        }



    /**
     * 递归查找子目录
     * @param dbid 当前菜单id
     * @param list 要查找的列表
     * @return
     */
    private JSONArray getChild(Integer dbid, List<ConfFlow> list) {
        // 子菜单
        JSONArray childArray = new JSONArray();
        for (ConfFlow flow : list) {
            // 遍历所有节点,将父菜单id与传过来的id比较
            if (flow.getInt("parentid") != null) {
                if (flow.getInt("parentid") == dbid) {
                    JSONObject childObject = new JSONObject();
                    childObject.put("name", flow.get("name", ""));
                    childObject.put("dbid", flow.getInt("id"));
                    childObject.put("level", flow.getInt("level"));
                    childObject.put("type", "icon-minus-sign");
                    childObject.put("parentdbid", dbid);
                    childObject.put("child", new JSONArray());
                    childArray.add(childObject);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (Object childObject : childArray) {
            JSONObject jsonObject = (JSONObject) childObject;
            jsonObject.put("child", getChild(jsonObject.getInteger("dbid"), list));
        }
        // 递归退出条件
        if (childArray.size() == 0) {
            return new JSONArray();
        }
        return childArray;
    }

最后:对拿到的json进行解析,显示到前端页面即可

补充java代码:

/**
     * 递归删除
     * @param id 需删除的id
     * @param list 查询的list
     * @return
     */
    private boolean delChild(Integer id, List<ConfFlow> list) {
        Set<Integer> needDelChildId = new HashSet<>();
        for(ConfFlow flow : list){
            if (flow.getInt("parentid") != null) {
                if (flow.getInt("parentid") == id) {
                    needDelChildId.add(flow.getInt("id"));
                }
            }
        }
        Iterator<Integer> integerIterator =  needDelChildId.iterator();
        while (integerIterator.hasNext()){
            boolean flag = delChild(integerIterator.next(), list);
            if (flag){
                integerIterator.remove();
            }
        }

        if (needDelChildId.size() == 0){
            return service.tryDelete(id, new ConfFlow(), this); // 删除(自行替换)
        }

        return true;
    }

参考:https://blog.youkuaiyun.com/frankcheng5143/article/details/52958486?utm_source=blogxgwz1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值