Javascript 在 jsTree 中,如何通过节点 id 获取节点信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10390150/
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
In jsTree , How to get Node information by node id?
提问by StackOverFlow
In jsTree ,How to get Node information by node id ?
在jsTree中,如何通过节点id获取节点信息?
I know id of following node i.e 295 then how to get complete node information
我知道以下节点的 id,即 295 那么如何获取完整的节点信息
<item id="295" parent_id="192" title="itemTitle" version="1">
<content><name>Bhushan Sambhus</name></content>
</item>
above xml part rendered into jsTree is as follows
上面渲染成jsTree的xml部分如下
$("#treeViewDiv").jstree({
"xml_data" : {
"data" : "" +
"<root>" +
"<item id="295" parent_id="192" title="itemTitle" version="1">"+
"<content><name>Bhushan Sambhus</name></content> "+
"</item>"
}
"plugins" : [ "themes", "xml_data","ui" ]
});
Something like following psudo code
类似于以下伪代码
function getNodeByNodeID(node_id){
// some code
// $.jstree.get_node ...... etc ?
//
return relatedNodeInformation;
}
var nodeInfo = getNodeByNodeID(providedNodeID) // psudo code
// any api in jstree to get nodeInfo by providedNodeID?
var parent_id_value = nodInfo.attr("parent_id");
var title_value = nodInfo.attr("title");
var version_value = nodInfo.attr("version");
var node_name = nodInfo.children("a").text()
alert(parent_id_value+" :: "+title_value+" :: "+version_value+" :: "+node_name);
Input : 295
输入:295
Output: 192 :: node_name :: 1 :: node_name
输出:192 :: node_name :: 1 :: node_name
Any help or guidance in this matter would be appreciated
在这方面的任何帮助或指导将不胜感激
回答by Daniel Bidulock
If I'm understanding your question correctly, you can accomplish what you want to do like this:
如果我正确理解你的问题,你可以像这样完成你想做的事情:
var nodInfo = $("#" + providedNodeId);
var parent_id_value = nodInfo.attr("parent_id");
var title_value = nodInfo.attr("title");
var version_value = nodInfo.attr("version");
var node_name = nodInfo.children("a").text();
alert(parent_id_value+" :: "+title_value+" :: "+version_value+" :: "+node_name);
回答by Frog Pr1nce
Just want to help keep the answer up-to-date. Using jstree 3.1.0, node objects (not DOM objects) are fetched by using this code:
只是想帮助保持答案最新。使用 jstree 3.1.0,使用以下代码获取节点对象(不是 DOM 对象):
var treeMain; // reference holder
$(document).ready( function () { // when the DOM is ready
treeMain = $('#treeMenus').jstree(); // create the tree and get the reference
});
function getNode( sNodeID)
{
return $.jstree.reference(treeMain).get_node(sNodeID); // use the tree reference to fetch a node
}
I've seen several answers to this question on StackOverflow that all talk about getting back to the DOM object of a tree item. I'm willing to bet that most people asking this question really want to get back to the underlying JSON data object of a tree item, which is why they say they want the node object (which has the .original property). Specifically, you need this for implementing things like "create" functionality where you need to create a new JSON data object with a ParentID that is set to the ID of the parent JSON data object. I searched for 2 days and didn't find anything clear in the jstree documentation that explained this:
我在 StackOverflow 上看到了这个问题的几个答案,它们都在谈论返回到树项的 DOM 对象。我敢打赌,问这个问题的大多数人真的想回到树项的底层 JSON 数据对象,这就是为什么他们说他们想要节点对象(具有 .original 属性)。具体来说,您需要使用它来实现诸如“创建”功能之类的功能,您需要使用设置为父 JSON 数据对象 ID 的 ParentID 创建新的 JSON 数据对象。我搜索了 2 天,在 jstree 文档中没有找到任何明确的解释:
$.jstree.reference(treeMain).get_node(sNodeID);
simple call. In their defense, they do have a 1 line example buried in here:
简单的调用。在他们的辩护中,他们确实有一个埋藏在这里的 1 行示例:
http://www.jstree.com/docs/interaction/
http://www.jstree.com/docs/interaction/
but it's an example most people won't care about (the user will be selecting nodes most of the time), and certainly not clear for what it is actually capable of doing. Anyways... hope this is helps save someone else a couple of days. =)
但这是一个大多数人不会关心的例子(用户大部分时间都会选择节点),当然也不清楚它实际上能够做什么。无论如何......希望这有助于拯救其他人几天。=)