如何在 jquery jstree 中获取已检查的节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18268306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get checked nodes in jquery jstree
提问by Shibankar
I have created one jquery jstree and it's working fine. Now the problem is how to get the the checked nodes details.
我创建了一个 jquery jstree,它运行良好。现在的问题是如何获取已检查节点的详细信息。
For Creating JStree The code is:
对于创建 JStree 的代码是:
$(function () {
$("#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 while getting checked nodes i need all the attributes values for those checked elements. Say like for "tags" the json object looks like {"data":"tags","id":30,"pid":0}, so if user select tag i need the value of "data" And "id". i have tried to write some code but unfortunately that is not working.
现在,在获取已检查节点时,我需要这些已检查元素的所有属性值。比如说“标签”,json 对象看起来像 {"data":"tags","id":30,"pid":0},所以如果用户选择标签,我需要“data”和“id”的值. 我曾尝试编写一些代码,但不幸的是这不起作用。
Getting Checked Nodes.
获取检查节点。
$("#" +div2.childNodes[i].id).jstree("get_checked",null,true).each
(function () {
alert(this.data);
alert(this.id);
});
Kindly give me a solution.
请给我一个解决方案。
回答by leole
As the Author of jstree (Ivan Bozhanov) points out on google-Groups Discussion regarding get_checked, it can also be achieved using the following:
正如 jstree 的作者 (Ivan Bozhanov) 在关于 get_checked 的 google-Groups Discussion上指出的那样,也可以使用以下方法来实现:
$('#tree').jstree(true).get_selected();
This returns a List of the IDs, e.g. ["j1_2"] or ["j1_2", "j1_3", "j1_1"]
这将返回一个 ID 列表,例如 ["j1_2"] 或 ["j1_2", "j1_3", "j1_1"]
Check out the fiddle by Ivan Bozhanov himself on: jsfiddle-Example get_selected
查看 Ivan Bozhanov本人的小提琴:jsfiddle-Example get_selected
回答by Vaibs_Cool
function submitMe(){
var checked_ids = [];
$("#server_tree").jstree("get_checked",null,true).each
(function () {
checked_ids.push(this.id);
});
doStuff(checked_ids);
Go through this once jstree google groups
通过这一次 jstree 谷歌组
回答by wtf512
$.each($("#jstree_demo_div").jstree("get_checked",true),function(){alert(this.id);});
回答by hrishikesh
$('#dvTreeStructure').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]).text.trim());
}
alert('Selected: ' + r.join(', '));
}
回答by Asif Nowaj
While using get_checked
or get_selected
pass the boolean as false to get the whole node where if you send as true, it will return only node Ids.
在使用get_checked
或get_selected
将布尔值传递为 false 以获取整个节点时,如果发送为 true,它将仅返回节点 ID。
You have a look at https://www.jstree.com/api/#/?q=checkbox&f=get_checked([full])
你看看https://www.jstree.com/api/#/?q=checkbox&f=get_checked([full])
You can also have a look at https://everyething.com/Example-of-jsTree-to-get-all-checked-nodesto get an idea of different kind of selected.
您还可以查看https://everyething.com/Example-of-jsTree-to-get-all-checked-nodes以了解不同类型的选择。