我已经在json对象中添加了索引节点现在我需要在js树上获取该索引单击event.here obj.newAttributeThreatName来自服务,我希望在树结构中显示。请检查下面提供的代码:< / p>
var treeData = [];
var threatIndex = [];
var folders = {};
threatsArr.forEach(function(obj) {
if (obj.newAttributeThreatName in folders) {
console.log("if")
treeData[folders[obj.newAttributeThreatName]].children.push({
text : obj.newAttributeThreatName,
index : obj.newAttributeThreatIndex
});
} else {
console.log("else");
folders[obj.newAttributeThreatName] = treeData.push({
text : obj.newAttributeThreatName,
index : obj.newAttributeThreatIndex,
children : [ {
text : obj.newAttributeThreatName,
index : obj.newAttributeThreatIndex
} ]
}) - 1;
}
});
var myJsonString = JSON.stringify(treeData);
var obj123 = JSON.parse(myJsonString);
$('#listThreat').jstree({
'core' : {
'data' : obj123
},
"plugins" : [
"wholerow", "changed",
]
});
$('#listThreat').on('changed.jstree', function(e, data) {
var i, j, r = [];
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).index);
}
console.log($("#listThreat").jstree("get_selected",true).attr("index"));
console.log('Selected: ' + r.join(', '));
$('#resultText').html('Selected: ' + r.join(', '));
}).jstree();