<div id="updateFormBlockDiv" class="layui-input-block"> <ul id="menuTree2" class="dtree" data-id="0"></ul> </div> var data = { 0: {YY: "string0", HLS: "string0", YZ: "string0"}, 1: {DV: "string1", TYH: "string1", NM: "string1"}, 2: {NG: "string2", EFY: "string2", WRE: "string2"}, }; var treeData = []; for (var key in data) { if (data.hasOwnProperty(key)) { var node = { id: key, title: key, children: [] }; for (var prop in data[key]) { if (data[key].hasOwnProperty(prop)) { node.children.push({ id: prop, title: prop //data[key][prop] }); } } treeData.push(node); } } console.log(treeData, 'treeDatatreeData') dtree.render({ elem: '#menuTree2', data: treeData, skin: "blueStyle", // ficon: ["2", "-1"], // 隐藏二级图标 // icon: '-1', // 隐藏二级图标 select: true, toolbar: true, }); 为何无法收缩了
时间: 2023-07-19 07:23:20 浏览: 144
layui菜单管理(树结构)
3星 · 编辑精心推荐
根据您提供的代码,您的树形结构数据中的每个节点都没有子节点,因此无法展开或收缩。您可以通过修改`node.children`数组中的数据,将每个节点的子节点添加到其中,以便展开或收缩节点。例如,您可以修改代码如下:
```
var data = {
0: {YY: "string0", HLS: "string0", YZ: "string0"},
1: {DV: "string1", TYH: "string1", NM: "string1"},
2: {NG: "string2", EFY: "string2", WRE: "string2"},
};
var treeData = [];
for (var key in data) {
if (data.hasOwnProperty(key)) {
var node = { id: key, title: key, children: [] };
for (var prop in data[key]) {
if (data[key].hasOwnProperty(prop)) {
node.children.push({ id: prop, title: prop, children: [] });
}
}
treeData.push(node);
}
}
console.log(treeData, 'treeDatatreeData')
dtree.render({
elem: '#menuTree2',
data: treeData,
skin: "blueStyle",
select: true,
toolbar: true,
});
```
在上面的代码中,我们将每个子节点的`children`属性设置为空数组,以便在需要时添加子节点。这样,您就可以展开或收缩节点了。
阅读全文