Bootstrap实践之JavaScrpt递归转换treeview结构数据
Bootstraptree菜单admin 发布于:2020-04-19 02:46:54
阅读:loading
前文中采用Java递归实现的一个数据转换,跟前台JavaScript比较起来,还是使用JavaScript比较好,比较一旦数据中的上下级节点如果存在错误的结构可能会导致死循环,最终直接出现递归溢出的问题,影响服务器正常运行,而这种逻辑放在前台比较起来影响的范围就小许多了,故本文将前一篇的使用Java实现又改造成使用JavaScript实现,参考如下数据所示:
[
{ "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": "湖北省"
}
]
function convertData(){
var datas = [
{"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}
];
var treeviews = convertTreeview(datas);
for (var i=0 , lens = treeviews.length ; i < lens ; i++){
var treeview = treeviews[i];
collectList(treeview , datas);
}
return treeviews;
}
function collectList(treeview , datas){
var nodeId = treeview.nodeId;
for (var i=0 , lens = datas.length ; i < lens ; i++) {
var data = datas[i];
var parentId = data.parentId;
if(parentId != null && parentId === nodeId){
if(treeview.nodes == null){
treeview.nodes = [];
}
var childMenuTreeview = {"nodeId" : data.id , "text": data.name , state: null , nodes: null};
treeview.nodes.push(childMenuTreeview);
this.collectList(childMenuTreeview , datas);
}
}
}
function convertTreeview(datas){
var data = [];
for (var i=0 , lens = datas.length ; i < lens ; i++){
var item = datas[i];
if(item.parentId === -1){
var treeview = {"nodeId": item.id , "text": item.name , state: null , "nodes": null};
data.push(treeview);
}
}
return data;
}
在线预览地址为:“/demo/bootstrap/treeview/data-convert-js.html”
点赞
发表评论
评论列表
留言区
- 给项目添加文件在线编辑功能
- 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实践之Java递归转换treeview结构数据
- 彻底搞定 tree 菜单
- ztree的最新实现