layui 2.9版本自带treeTable插件但是无法加载出子级 解决

2.8以下的可以使用treetable-lay插件,使用layui 2.8以上的版本则已经内嵌插件,不可以再额外加载插件。

layui treeTable官方文档:树表组件 treeTable - Layui 文档

正确的treetable数据格式(已简化):

{
  "code": 0,
  "count": 1000,
  "data": [
    {
      "id": 1,
      "name": "User-1",
      "type": 4,
      "status": 2,
      "score": 28,
      "experience": 54981,
      "sex": "男",
      "city": "丽江市",
      "description": "-",
      "createTime": "2014-06-04 12:29:55",
      "parentId": null,
      "children": [
        {
          "id": 2,
          "name": "User-2",
          "type": 2,
          "status": 3,
          "score": 75,
          "experience": 43884,
          "sex": "女",
          "city": "大理白族自治州",
          "description": "-",
          "createTime": "1971-11-03 19:15:43",
          "parentId": null,
          "children": [
            {
              "id": 3,
              "name": "User-3",
              "type": 2,
              "status": 3,
              "score": 72,
              "experience": 75912,
              "sex": "女",
              "city": "渭南市",
              "description": "-",
              "createTime": "2022-01-17 01:48:02",
              "parentId": null,
              "children": [
                {
                  "id": 4,
                  "name": "User-4",
                  "type": 3,
                  "status": 3,
                  "score": 21,
                  "experience": 23198,
                  "sex": "男",
                  "city": "海外",
                  "description": "-",
                  "createTime": "1986-11-06 23:56:45",
                  "parentId": null,
                  "children": [],
                  "isParent": false
                },
                {
                  "id": 6,
                  "name": "User-6",
                  "type": 2,
                  "status": 4,
                  "score": 70,
                  "experience": 51144,
                  "sex": "男",
                  "city": "益阳市",
                  "description": "-",
                  "createTime": "2017-02-21 00:26:02",
                  "parentId": null,
                  "children": [],
                  "isParent": false
                }
              ],
              "isParent": true
            },
            {
              "id": 7,
              "name": "User-7",
              "type": 5,
              "status": 3,
              "score": 24,
              "experience": 64919,
              "sex": "男",
              "city": "贵港市",
              "description": "-",
              "createTime": "1981-05-14 17:52:03",
              "parentId": null,
              "children": [
                {
                  "id": 8,
                  "name": "User-8",
                  "type": 6,
                  "status": 2,
                  "score": 2,
                  "experience": 80656,
                  "sex": "男",
                  "city": "宿迁市",
                  "description": "-",
                  "createTime": "1984-05-24 20:44:26",
                  "parentId": null,
                  "children": [],
                  "isParent": false
                }
              ],
              "isParent": true
            }
          ],
          "isParent": true
        }
      ],
      "isParent": true
    },
    {
      "id": 9,
      "name": "User-9",
      "type": 6,
      "status": 2,
      "score": 58,
      "experience": 2414,
      "sex": "女",
      "city": "宿州市",
      "description": "-",
      "createTime": "2015-05-06 00:39:19",
      "parentId": null,
      "children": [],
      "isParent": false
    },
    {
      "id": 325,
      "name": "User-325",
      "type": 3,
      "status": 3,
      "score": 62,
      "experience": 94793,
      "sex": "女",
      "city": "嘉峪关市",
      "description": "-",
      "createTime": "1990-02-26 16:03:01",
      "parentId": null,
      "children": [
        {
          "id": 326,
          "name": "User-326",
          "type": 4,
          "status": 4,
          "score": 70,
          "experience": 19861,
          "sex": "男",
          "city": "佳木斯市",
          "description": "-",
          "createTime": "1971-09-13 13:37:06",
          "parentId": null,
          "children": [
            {
              "id": 327,
              "name": "User-327",
              "type": 1,
              "status": 2,
              "score": 10,
              "experience": 1866,
              "sex": "女",
              "city": "重庆市",
              "description": "-",
              "createTime": "1991-08-01 14:53:52",
              "parentId": null,
              "children": [
                {
                  "id": 328,
                  "name": "User-328",
                  "type": 6,
                  "status": 3,
                  "score": 97,
                  "experience": 81309,
                  "sex": "女",
                  "city": "重庆市",
                  "description": "-",
                  "createTime": "1973-06-11 16:06:26",
                  "parentId": null,
                  "children": [],
                  "isParent": false
                }
              ],
              "isParent": true
            }
          ],
          "isParent": true
        }
      ],
      "isParent": true
    }
  ]
}

给父数据嵌入children:

    public Map<String, Object> infoMenus(){
        Map<String, Object> map = new HashMap();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", menu.getCountMenu());
        map.put("data", buildTree(menu.infoMenus(),0));
        System.out.println(map);
        return map;
    }

    List<Map<String, Object>> buildTree(List<Map<String, Object>> list, int parentId) {
        List<Map<String, Object>> children = new ArrayList<>();
        for (Map<String, Object> item : list) {
            int currentId = (int) item.get("Menu_ID");
            int currentParentId = (int) item.get("parentId");
            if (currentParentId == parentId) {
                Map<String, Object> child = new HashMap<>(item);
                child.put("children", buildTree(list, currentId)); // 递归处理子节点
                children.add(child);
            }
        }
        return children;
    }

原理:数据格式要求严格,回传的数据有四个key必须完全一致,大小写也要一致

1.children:子级数据,就是每个父级下应该有的内容

2.isParent:是否是父级:是:true;不是:false

 3.name:点击的内容

 4.parentId:父节点,子级必填

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值