How do I get the id of the selected node in jsTree?

前端 未结 14 2322
北恋
北恋 2020-12-29 22:38

How can I get the id of the selected node in a jsTree?

function createNewNode() {
  alert(\'test\');
  var tree = $.tree.reference(\"#basic_html\");
  select         


        
相关标签:
14条回答
  • 2020-12-29 23:11

    In jstree version 3.1.1, you can get it directly from get_selected:

    $("#<your tree container's id>").jstree("get_selected")
    
    0 讨论(0)
  • 2020-12-29 23:16
      $.jstree._reference('#my_tree_container')._get_node(null, true).each(function() {
        id = $(this).attr("id");
        alert('Id selected: ' + id);        
      });
    
    0 讨论(0)
  • 2020-12-29 23:19

    With latest version of Jstree; you can do it as following:

    <script type="text/javascript>
        var checked_ids = [];
        $('#your-tree-id).jstree("get_checked",null,true).each(function(){
            checked_ids.push(this.id);
        });
        alert(checked_ids.join(","));
    </script>
    
    0 讨论(0)
  • 2020-12-29 23:22

    Nodes in jsTree are essentially wrapped list items. This will get you a reference to the first one.

    var n = $.tree.focused().get_node('li:eq(0)')
    

    You can replace $.tree.focused() if you have a reference to the tree.

    To get the id, take the first matched element

    if (n.length)
        id = n[0].id
    

    or you can use the jQuery attr function, which works on the first element in the set

    id = n.attr('id');
    
    0 讨论(0)
  • 2020-12-29 23:24
    <script type="text/javascript>
        checked_ids.push($(this).context.id);
    </script>
    
    0 讨论(0)
  • 2020-12-29 23:24

    In some cases and/or jstree versions this solution doesn't work.

    $('#tree').jstree('get_selected').attr('id');
    

    Instead of defined "id" I get nothing. What did the trick for me is:

    $("#tree").jstree("get_selected").toString();
    
    0 讨论(0)
提交回复
热议问题