2015-04-23 55 views
1

我需要通过属性名称在jstree中选择一个节点。然后选择该节点后,我需要得到它的ID。 基本上我想得到一个节点的id给定它的名字。jstree:通过名称属性而不是id来选择jstree中的一个节点

我尝试下面的代码:

$("#tree1").bind("loaded.jstree", function (e, data) { 

var isa_name = "ISA16469"; 

//$("#tree1").jstree("select_node", "#01"); //this code works but it selects by id, I want to select by attribute name 

$("#tree1").jstree("select_node", $("li[name='" + isa_name + "']")); //doesn't work 

var test = $('#tree1').jstree('get_selected').attr('id'); //get id of selected node 
alert(test) 

}) 

你的帮助是最欢迎的。非常感谢

回答

3

当使用ID作为选择器(在jstree函数中)时,请不要提供前导#,仅使用ID。

至于这个问题 - 不幸的是,这将无法正常工作,因为jstree只在DOM中保留可见节点,这意味着如果您的LI节点没有显示(例如,如果它的父节点被关闭),您将无法找到它在DOM中。

我不确定在LI节点上具有名称属性是否有效,但无论如何 - 如果您坚持以这种方式查找节点,您将不得不遍历内部jstree模型。这里是你如何做到这一点: http://jsfiddle.net/DGAF4/450/

0

我认为这将有助于

<div id="jstree"></div> 

$("#jstree").on('ready.jstree', function() { 
var instance = $("#jstree").jstree(true); 
var branchCont = instance._model.data; 

for(var branchKey in branchCont) { 

    var branch = branchCont[branchKey]; 
    if(branch.text && branch.text === "Child node 1") { 

    instance.select_node(branchKey); 
    break; 
    } 
} 
}); 

$("#jstree").jstree({ 
'core' : { 
    'data' : [ 
     { "text" : "Root node", "children" : [ 
      { "text" : "Child node 1" }, 
      { "text" : "Child node 2" } 
     ] 
     }, 
     { "text" : "Root node2", "children" : [ 
      { "text" : "Child node B1" }, 
      { "text" : "Child node B2" } 
     ] 
     } 
    ] 
} 
}); 

https://jsfiddle.net/shubojs/qj8hty83/3/