Bootstrap实践之Java递归转换treeview结构数据
Bootstraptree菜单admin 发布于:2020-04-19 02:36:27
阅读:loading
前文中我表示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 }
]
[
{
"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));
}
}
注意:本程序存在Java后端代码的递归调用,可能会出现由数据的上下级导致的循环调用,即递归溢出,正常来说需要考虑递归的深度问题。当前博客的源代码中存在此类工具类,包含有递归深度的限制,有需要自行查找。
点赞
发表评论
评论列表
留言区
- 给项目添加文件在线编辑功能
- Bootstrap最佳实践之开篇
- Bootstrap实践之tab插件
- Bootstrap实践之弹出框插件(一)
- Bootstrap实践之弹出框插件(二)
- Bootstrap实践之下拉框插件
- Bootstrap实践之自动补全插件
- Bootstrap实践之table应用
- Bootstrap-table基础篇(基于js构建表格)
- Bootstrap-table基础篇(分页实现)
- Bootstrap-table基础篇(小技巧整理)
- Bootstrap-table进阶篇(单元格合并)
- Bootstrap-table进阶篇(动态列)
- Bootstrap-table进阶篇(冻结列)
- Bootstrap-table进阶篇(列宽拖动)
- Bootstrap-table进阶篇(树形表格)
- Bootstrap实践之滚动条插件
- Bootstrap实践之文件上传插件
- Bootstrap实践之treeview插件
- Bootstrap实践之JavaScrpt递归转换treeview结构数据
- 彻底搞定 tree 菜单
- ztree的最新实现