登录 
欢迎来到陈冬冬(雪地里走)的学习天地 | 经验分享整理平台 | 陈冬冬
 服务器本次启动于:7个月前 

Bootstrap实践之Java递归转换treeview结构数据

2020-04-19 02:36:27 阅读:()

前文中我表示treeview使用的数据格式,需要多层级节点嵌套的结构,而非所有菜单均为一级的数据格式,在使用的时候,我们不得不需要将一个一层结构的数据再转换成多层级的实现,参考如下数据所示:

① 初始数据结构

[
  {
   
"id": 1,
   
"name": "北京市",
   
"parentId": -1
 
},
  {
   
"id": 2,
   
"name": "海淀区",
   
"parentId": 1
 
},
  {
   
"id": 3,
   
"name": "朝阳区",
   
"parentId": 1
 
},
  {
   
"id": 4,
   
"name": "湖北省",
   
"parentId": -1
 
},
  {
   
"id": 5,
   
"name": "武汉市",
   
"parentId": 4
 
},
  {
   
"id": 6,
   
"name": "襄阳市",
   
"parentId": 4
 
},
  {
   
"id": 7,
   
"name": "襄城区",
   
"parentId": 6
 
},
  {
   
"id": 8,
   
"name": "襄州区",
   
"parentId": 6
 
},
  {
   
"id": 9,
   
"name": "樊城区",
   
"parentId": 6
 
},
  {
   
"id": 10,
   
"name": "老河口市",
   
"parentId": 6
 
},
  {
   
"id": 11,
   
"name": "谷城县",
   
"parentId": 6
 
},
  {
   
"id": 12,
   
"name": "南漳县",
   
"parentId": 6
 
},
  {
   
"id": 13,
   
"name": "枣阳市",
   
"parentId": 6
 
},
  {
   
"id": 14,
   
"name": "保康县",
   
"parentId": 6
 
},
  {
   
"id": 15,
   
"name": "宜城市",
   
"parentId": 6
 
}
]

 

② treeview需要的数据结构

[
  {
   
"nodeId": 1,
   
"nodes": [
      {
       
"nodeId": 2,
       
"text": "海淀区"
     
},
      {
        
"nodeId": 3,
       
"text": "朝阳区"
     
}
    ],
   
"text": "北京市"
 
},
  {
   
"nodeId": 4,
   
"nodes": [
      {
       
"nodeId": 5,
       
"text": "武汉市"
     
},
      {
       
"nodeId": 6,
       
"nodes": [
          {
           
"nodeId": 7,
           
"text": "襄城区"
         
},
          {
           
"nodeId": 8,
           
"text": "襄州区"
         
},
          {
           
"nodeId": 9,
           
"text": "樊城区"
         
},
          {
           
"nodeId": 10,
           
"text": "老河口市"
         
},
          {
           
"nodeId": 11,
           
"text": "谷城县"
         
},
          {
           
"nodeId": 12,
           
"text": "南漳县"
         
},
          {
           
"nodeId": 13,
           
"text": "枣阳市"
         
},
          {
           
"nodeId": 14,
           
"text": "保康县"
         
},
          {
           
"nodeId": 15,
           
"text": "宜城市"
         
}
        ],
       
"text": "襄阳市"
     
}
    ],
   
"text": "湖北省"
 
}
]

参考代码

package cn.chendd.tips.examples.treeview;

import ...;

/**
 *
数据转换测试
 
*
 * @author chendd
 * @date 2020/x/xx 22:34
 */
public class DataConvertTest {

   
private Integer ROOT_ID = -1;//约定根节点为-1
   
private List<City> cityList;

   
@Test
   
public void test(){
        System.
out.println("======原始数据======");
        System.
out.println(JSONObject.toJSONString(cityList , true));
        Stream<City> rootCity =
cityList.stream().filter(city -> ROOT_ID.equals(city.getParentId()));
        List<Treeview> treeviewList =
new ArrayList<>();
       
this.convertTreeview(rootCity, treeviewList);
       
for (Treeview treeview : treeviewList) {
           
this.collectList(treeview , this.cityList);
        }
        System.
out.println("======转换后的数据======");
        System.
out.println(JSON.toJSONString(treeviewList , true));
    }

   
/**
     *
获取原始数据中的一级菜单
    
*/
   
private void convertTreeview(Stream<City> cityStream, List<Treeview> treeviewList) {
        cityStream.forEach(city -> {
            Treeview treeview =
new Treeview();
            treeview.setNodeId(city.getId());
            treeview.setText(city.getName());
            treeview.setState(
null);
            treeview.setNodes(
null);
           
treeviewList.add(treeview);
        });
    }

   
/**
     *
遍历一级菜单,并递归调用字级菜单,查找对应的菜单节点
    
*/
   
private void collectList(Treeview treeview, List<City> cityList) {
        Integer nodeId = treeview.getNodeId();
       
for (City city : cityList) {
            Integer parentId = city.getParentId();
           
if(parentId != null && parentId.equals(nodeId)){
               
if(treeview.getNodes() == null){
                    treeview.setNodes(
new ArrayList<>());
                }
                Treeview childMenuTreeview =
new Treeview();
                childMenuTreeview.setNodeId(city.getId());
                childMenuTreeview.setText(city.getName());
                childMenuTreeview.setState(
null);
                treeview.getNodes().add(childMenuTreeview);
               
this.collectList(childMenuTreeview , cityList);
            }
        }
    }

   
@Before
   
public void init(){
       
cityList = new ArrayList<>();
       
cityList.add(new City(1 , "北京市" , ROOT_ID));
       
cityList.add(new City(2 , "海淀区" , 1));
       
cityList.add(new City(3 , "朝阳区" , 1));
       
cityList.add(new City(4 , "湖北省" , ROOT_ID));
       
cityList.add(new City(5 , "武汉市" , 4));
       
cityList.add(new City(6 , "襄阳市" , 4));
       
cityList.add(new City(7 , "襄城区" , 6));
       
cityList.add(new City(8 , "襄州区" , 6));
       
cityList.add(new City(9 , "樊城区" , 6));
       
cityList.add(new City(10 , "老河口市" , 6));
       
cityList.add(new City(11 , "谷城县" , 6));
       
cityList.add(new City(12 , "南漳县" , 6));
       
cityList.add(new City(13 , "枣阳市" , 6));
       
cityList.add(new City(14 , "保康县" , 6));
       
cityList.add(new City(15 , "宜城市" , 6));
    }

}

源码下载

https://gitee.com/88911006/chendd-examples/tree/master/tips/test/main/java/cn/chendd/tips/examples/treeview


你可能感兴趣的:
      关键字:city(29)treeview(26)citylist(23)new(20)parentid(18)
      互动()
      评论()
      比起点赞,站长更喜欢登录后的评论
      • 0
      • 0
      • 0
      • 0
      • 0