Get checked item in jQuery JStree

I created one jquery jstree with a JSON object. My tree works fine

Jstree creation

$("#tree").jstree({ "json_data": { "data": [{ "data": "pe_opensourcescanning", "id": 0, "pId": -1, "children": [{ "data": "tags", "id": 30, "pid": 0 }, { "data": "branches", "id": 29, "pid": 0 }, { "data": "trunk", "id": 1, "pid": 0, "children": [{ "data": "import-export", "id": 28, "pid": 1 }, { "data": "custom_development", "id": 12, "pid": 1 }, { "data": "Connectors", "id": 7, "pid": 1 }, { "data": "support", "id": 6, "pid": 1 }, { "data": "Installation-Configuration", "id": 5, "pid": 1 }, { "data": "backup", "id": 2, "pid": 1 }] }] }] }, "plugins": ["themes", "json_data", "checkbox", "ui"] }).bind("select_node.jstree", function (e, data) { alert(data.rslt.obj.data("id")); }); 

Now I want the "id" and "data" values ​​for each checked node. I tried to write something, but unfortunately this does not work. Please help me how to achieve this goal.

Getting test nodes

 $("#tree").jstree("get_checked",null,true).each(function () { alert(this.data); alert(this.id); }); 
+6
source share
1 answer

Your function, in order to get checked nodes, is correct, the problem is that your JSON is not correct; to set properties on the tree, so they cannot be obtained from methods.

You must use the attr property or the data will not be set to node from the documents:

The attr object will be displayed as attributes as a result of li node.

code:

 $("#tree").jstree({ "json_data": { "data": [{ "data": "pe_opensourcescanning", "attr": { "id": 77, "pId": -1 }, "children": [{ "data": "tags", "attr": { "id": 30, "pid": 0 } }, { "data": { "title": "branches" }, "attr": { "id": 29, "pid": 0 } }] }] }, "plugins": ["themes", "json_data", "checkbox", "ui"] }) $("#getChecked").click(function () { $("#tree").jstree("get_checked", null, true).each(function (i, e) { console.log($(this).attr("id")) }); }); 

Demo: http://jsfiddle.net/IrvinDominin/RYhgD/

Docs: http://www.jstree.com/documentation/json_data

+2
source

Source: https://habr.com/ru/post/951907/


All Articles