如何使用数组对象填充jstree子项并在单击时将其恢复
如何填充子项并通过单击每个子项获取其内容
在下面的代码我想:
[{"cid":2091,"name":"name1","text":"Child 1"},{"cid":2095,"name":"name2","text":"A Child 2"},{"cid":2098,"name":"name3","text":"A Child 3"}]
代替 [ "Child 1", "A Child 2","A Child 3"]
我的意思是说如果我点击Child 1
我应该得到{"cid":2091,"name":"name1","text":"Child 1"}
这里是jsfiddle : http : //jsfiddle.net/2Jg3B/2102/
$('#jstree').jstree({ "json_data" : { "data" : [ { "data" : "A node", "metadata" : { id : 23 }, "children" : [ "Child 1", "A Child 2","A Child 3"] // replace with [{"cid":2091,"name":"name1","text":"Child 1"},{"cid":2095,"name":"name2","text":"A Child 2"},{"cid":2098,"name":"name3","text":"A Child 3"}] }, { "attr" : { "id" : "li.node.id1" }, "data" : { "title" : "Long format demo", "attr" : { "href" : "#" } } } ] }, "plugins" : [ "themes", "json_data", "ui" ] }); $("#jstree").bind( "select_node.jstree", function(evt, data){ if(data.inst._get_parent().length <= 0 || data.inst._get_parent()===-1){ console.log('data',data.inst.get_json()); console.log('data.node.id',data); }else{ var parent = data.inst._get_parent(); console.log(data.inst.get_json(parent)); } //selected node object: data.node; });
请注意:如果获得{"cid":2091,"name":"name1","text":"Child 1"}
点击Child 1等等..插件版本可以是new或者这个
请提前帮助我!
我不是100%确定这是否是正确的解决方案,但它确实有效。 基本上我所做的是用"data"
替换"text"
"data"
。
$('#jstree').jstree({ "json_data": { "data": [ { "data": "A node", "metadata": { id: 23 }, "children": [ { "data": "Child 1", "cid": 2091, "name": "name1", "metadata": { "id": 2091, "text": "Child 1" } }, { "data": "A Child 2", "cid": 2095, "name": "name2", "metadata": { "id": 2095, "text": "A Child 2" } }, { "data": "A Child 3", "cid": 2098, "name": "name3", "metadata": { "id": 2098, "text": "A Child 3" } } ] }, { "attr": { "id": "li.node.id1" }, "data": { "title": "Long format demo", "attr": { "href": "#" } } } ] }, "plugins": ["themes", "json_data", "ui"] }); $("#jstree").bind( "select_node.jstree", function(evt, data) { if (data.inst._get_parent().length <= 0 || data.inst._get_parent() === -1) { console.log('data', data.inst.get_json()); console.log('data.node.id', data); } else { console.log(data.inst.get_json()); } //selected node object: data.node; } );