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

发布于 2024-12-20 19:21:19 字数 281 浏览 2 评论 0原文

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

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

I have a jstree. I want to select the node which is bound to the object which has a location with id of 158. This works but seems stupid. What's the more idiomatic way of doing this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

情栀口红 2024-12-27 19:21:19

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

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

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

我在加载树后立即执行此操作,没有将其放入“就绪”绑定事件中,因为这似乎没有必要。

希望它能让一些人摆脱几个令人沮丧的小时。 。 。

加载后挂接到树上:

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

Just wanted to chime in here as none of the answers worked for me. What finally DID work was very simple:

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

Note that I didn't initialize someNodeId as a jQuery object. It's just a plain string.

I did this right after a tree was loaded without putting it into a "ready" bind event as it seems to not be necessary.

Hope it saves some one from a few frustrating hours. . .

To hook into the tree after it has been loaded:

.on('loaded.jstree', function() {
    // Do something here...
  });
暖伴 2024-12-27 19:21:19

基于 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"]
        }); 

是这样的是在之后吗?

Based on jsTree groups you can try

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

if the data looks like

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" : "#" }
         }
     }
 ] 

and jsTree set up

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"]
        }); 

Is that what you are after?

埋情葬爱 2024-12-27 19:21:19

做到了:

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

我用以下代码

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

I did it with:

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

this code:

jQuery.each(produto.categorias, function(i, categoria) {
    $('#lista-categorias').jstree(true).select_node(categoria.dadoCategoria.id);
});
夜还是长夜 2024-12-27 19:21:19

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

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

I was able to simulate a click on a jstree node as an alternative way to select a node.
The following code is what was used :

$(treeIdHandle + " li[id=" + nodeId + "] a").click();
眼泪都笑了 2024-12-27 19:21:19

如果您使用 HTML 而不是 JSON 数据填充树,并且想知道如何设置 node_id,则只需设置

  • 元素的 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/

    在此处输入图像描述

    If you're populating the tree using HTML instead of JSON data and wondering how to set the node_id, you just need to set the id attribute of the <li> element!

    <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>
    

    Then

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

    will select the Node 3 - Level 2 node.

    For those getting javascript errors, remember to use Full version of jQuery, not the slim version!

    For all down voters, here is the demo to prove it's working:
    https://jsfiddle.net/davidliang2008/75v3fLbs/7/

    enter image description here

    海夕 2024-12-27 19:21:19

    我使用jstree 3.0.8。不要使用“状态”

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

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

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

    i use jstree 3.0.8. don't use 'state'

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

    and server offer the json, the selected node has

    "state":{"selected":true,"opened":true}
    
    浅唱々樱花落 2024-12-27 19:21:19

    这个解决方案对我有用

    // 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;?>
    });
    

    希望这对你有用。

    This solution Works for me

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

    and even in a php loop (dynamically) :

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

    Hope this works for you.

    拥抱我好吗 2024-12-27 19:21:19

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

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

    希望它能工作:)

    i think u should write code to select node after jstree initialized, therefore use this code

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

    hope its work :)

    触发点击第一个锚点

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

    或按节点 ID 158

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

    trigger click on first anchor

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

    or by node id 158

    $("#jstree #158").find(".jstree-anchor:first").click(); 
    
    $('#' + 158).find(".jstree-anchor:first").click(); 
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文