$('#tt').tree({
url: 'test.json',
loadFilter: function (data) {
return data.result;
},
onClick: function (node) {
add()
}
});
var id = 7;
function add(){
var selected = $('#tt').tree('getSelected')
$('#tt').tree('append', {
parent: selected.target,
data: [
{
id: ++id,
text: "子节点"
}
]
});
}
刚开始是按loadFilter来处理json 数据的方式,点击节点后,节点图标变为了文件夹形式,但是子节点并没有添加上去。
找了很久原因,当时项目要演示,没太多时间研究,就放弃了,选用了其他的树形结构插件。后来还是觉得一定可以的又抽空琢磨了下。就用了给属性data赋值的形式。成功了!٩(๑>◡<๑)۶ 附上代码和测试数据
$.ajax({
type: "get",
url: "test.json",
dataType:'json',
success: function (data) {
$('#tt').tree({
data: data.result,
onClick:function(){
add()
}
});
},
error: function (e) {
console.log(e);
}
});
var id = 7;
function add(){
var selected = $('#tt').tree('getSelected')
$('#tt').tree('append', {
parent: selected.target,
data: [
{
id: ++id,
text: "子节点"
}
]
});
}
test.json
{
"result":[
{"id": 0,"text": "全部","children": [
{"id": "1","text": "node1"
},
{"id": 2, "text": "node2"
},
{"id": "3", "checked": true,"text": "node3"
},
{"id": "4","text": "node4","children": [
{"id": "5","text": "node4-1","iconCls": "icon-save"
},
{"id": "6","text": "node4-1"
}
]
}
]
}
]
}