如何以编程方式选择特定节点?

时间:2011-12-11 18:27:27

标签: jstree

我有一个jstree。我想选择绑定到id为158的位置的对象的节点。这可行,但似乎很愚蠢。这样做的惯用方法是什么?

var $tree = $('.jstree', myContext),
    node = $tree.find('li').filter(function() { 
        return ( $(this).data().location || {}).id === 158;
    });
$tree.jstree('select_node', n)

9 个答案:

答案 0 :(得分:36)

只是想在这里说话,因为没有一个答案对我有用。最后DID的工作非常简单:

$('#someTree').jstree('select_node', 'someNodeId');

请注意,我没有将someNodeId初始化为jQuery对象。它只是一个简单的字符串。

我在加载树之后立即执行此操作,而将其放入" ready"绑定事件似乎没有必要。

希望它可以从几个令人沮丧的时间中节省一些。 。

在加载后挂钩到树中:

.on('loaded.jstree', function() {
    // Do something here...
  });

答案 1 :(得分:20)

基于jsTree groups,您可以尝试

jQuery("#getStartedTree").jstree("select_node", "#test2"); 

如果数据看起来像

The JSON in the TextFile.txt - borrowed from your simple example
 [
     {
     "data" : "A node",
     "children" : [ "Child 1", "Child 2" ]
     },
     {
     "attr" : { "id" : "test1" },
         "data" : {
             "title" : "Long format demo",
             "attr" : { "id" : "test2", "href" : "#" }
         }
     }
 ] 

和jsTree设置

My tree container is <div id="getStartedTree">

My jsTree code
 $("#getStartedTree").jstree({
            "themes": {
                "theme": "default",
                "url": "../App_Css/Themes/Default/style.css",
                "dots": true,
                "icons": true
            },
            "json_data": {
                "ajax": {
                    "url": "../SiteMaps/TextFile.txt",
                    "dataType": "json",
                    "data": function(n) {
                        return { id: n.attr ? n.attr("id") : 0 };
                    }
                }
            },
            "plugins": ["themes", "json_data", "ui"]
        }); 

这就是你追求的目标吗?

答案 2 :(得分:3)

我能够模拟jstree节点上的点击,作为选择节点的替代方法。 以下代码是使用的代码:

$(treeIdHandle + " li[id=" + nodeId + "] a").click();

答案 3 :(得分:2)

我使用的是jstree 3.0.8。不要使用&#39;州&#39;

'plugins' : ['dnd','sort','types','contextmenu','wholerow','ui']

和服务器提供json,所选节点有

"state":{"selected":true,"opened":true}

答案 4 :(得分:2)

我是这样做的:

$('.jstree').jstree(true).select_node('element id');

此代码:

jQuery.each(produto.categorias, function(i, categoria) {
    $('#lista-categorias').jstree(true).select_node(categoria.dadoCategoria.id);
});

答案 5 :(得分:2)

如果您使用HTML而不是JSON数据填充树,并想知道如何设置node_id,则只需设置<li>元素的id属性!

<div class="tree-menu">
    <ul class="menu">
        <li id="node_1">
            Node 1 - Level 1
           <ul class="menu">
               <li id="node_3">
                   Node 3 - Level 2
               </li>
           </ul>
        </li>
        <li id="node_2">
            Node 2 - Level 1
        </li>
    </ul>
</div>

然后

$('.tree-menu')
    .jstree(true)
    .select_node("node_3");

将选择Node 3 - Level 2节点。

对于那些出现javascript错误的人,请记住使用jQuery的完整版,而不是精简版!

对于所有羽绒选民,以下是证明其正常工作的演示: https://jsfiddle.net/davidliang2008/75v3fLbs/7/

enter image description here

答案 6 :(得分:1)

此解决方案适合我

// after the tree is loaded
$(".jstree").on("loaded.jstree", function(){
    // don't use "#" for ID
    $('.jstree').jstree(true).select_node('ElementId');
});

甚至在php循环中(动态):

$(".jstree").on("loaded.jstree", function(){
    <?php foreach($tree as $node): ?>
        $('.jstree').jstree(true).select_node('<?=$node?>');
    <?php endforeach;?>
});

希望这适合你。

答案 7 :(得分:0)

我认为您应该在jstree初始化后编写代码以选择节点,因此请使用此代码

 $('#jstree')
    .on('ready.jstree', function (e, data) {
       // do function after jstree initialized
      $('#jstree')
        .jstree(true)
        .select_node('nodeid');
    });

希望它的工作:)

答案 8 :(得分:-1)

触发点击第一个锚点

$("#jstree .jstree-anchor:first").click(); 

或按节点ID 158

$("#jstree #158").find(".jstree-anchor:first").click(); 

$('#' + 158).find(".jstree-anchor:first").click(); 
相关问题