easyUI组装两级树结构
这是组装的方式:
{
"open": [
{
"id": 46,
"pName": "天天速冻间",
"pPyName": "TTSDJ",
"pNumber": "DS202009280046",
"pCode": "0046",
"projectendflag": "0",
"creattime": "2020-09-28 09:48:23",
"pcategoryName": "",
"isOpenWarehouse": "0"
},
{
"id": 47,
"pName": "天天面食",
"pPyName": "TTMS",
"pNumber": "DS202009280047",
"pCode": "0047",
"projectendflag": "0",
"creattime": "2020-09-28 09:48:23",
"pcategoryName": "",
"isOpenWarehouse": "0"
}
],
"close": [
{
"id": 38,
"pName": "武汉万吨",
"pPyName": "WHWD,WHMD",
"pNumber": "DS202009270001",
"pCode": "0001",
"projectendflag": "1",
"creattime": "2020-09-27 12:01:00",
"pcategoryName": "",
"isOpenWarehouse": "0"
}
]
}
返回的JSON格式
$.get("请求接口", function(data) {
let date_da = data.result; //返回json串
let openData = []; //tree用到的数组
let open = []; //开始的项目
let open_da = {}; //tree最外层的对象
let opendachil = []; //tree最外层的数组
for (let j = 0; j < date_da.open.length; j++) { //循环开始的项目放入对象中
let open_da_children = {}; //easyUI tree json串最里面的对象
open_da_children.id = date_da.open[j].id; //获取id
open_da_children.text = date_da.open[j].pName; //获取pName
opendachil.push(open_da_children); //对象放入数组中
}
open_da.text = "施工项目"; //tree一级
open_da.children = opendachil; //tree第二级放入一级children
openData.push(open_da) //tree 一级对象放入json数组中
let close = [];
let close_da = {};
let closedachil = [];
for (let i = 0; i < date_da.close.length; i++) {
let close_da_children = {};
close_da_children.id = date_da.close[i].id;
close_da_children.text = date_da.close[i].pName;
closedachil.push(close_da_children);
}
close_da.text = "竣工项目";
close_da.children = closedachil;
openData.push(close_da)
$("#tree_wl").tree({data: openData}) //展示tree的json串
})