前端要求特殊格式怎么办 学这招!

构建树结构

前端要求特殊格式? 学这招解决大多前端 格式

写这个可 给我恶心坏了都

list套map,再套list???

[{
    id: 1,
    label: '一级 1',
    children: [{
        id: 4,
        label: '二级 1-1',
        children: [{
            id: 9,
            label: '三级 1-1-1',
        }, {
            id: 10,
            label: '三级 1-1-2'
        }]
    }]
}, {
    id: 2,
    label: '一级 2',
    children: [{
        id: 5,
        label: '二级 2-1'
    }, {
        id: 6,
        label: '二级 2-2'
    }]
}, {
    id: 3,
    label: '一级 3',
    children: [{
        id: 7,
        label: '二级 3-1'
    }, {
        id: 8,
        label: '二级 3-2'
    }]
}]

没办法解决呗~

public List<Map<String, Object>> getallWarningInformation() {
    List<Map<String, Object>> treeLists = new ArrayList<>();
    List<SysClassifyEntity> categoryList = sysClassifyDao.getWarningInformationById(0);
    if (null != categoryList && categoryList.size() > 0) {
        categoryList.forEach(item -> {
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("id", item.getId());
            itemMap.put("label", item.getName());
            getTreeLists(itemMap, item.getId());
            treeLists.add(itemMap);
        });
    }
    return treeLists;
}

private void getTreeLists(Map<String, Object> pMap, Integer pid) {
    List<Map<String, Object>> itemLists = new ArrayList<>();
    List<SysClassifyEntity> categoryList = sysClassifyDao.getWarningInformationById(pid);
    if (null != categoryList && categoryList.size() > 0) {
        categoryList.forEach(item -> {
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("id", item.getId());
            itemMap.put("label", item.getName());
            getTreeLists(itemMap, item.getId());
            itemLists.add(itemMap);
        });
        pMap.put("children", itemLists);
    }
}
原文地址:https://www.cnblogs.com/laowt/p/14465419.html