从jstree中获取选定的Node

问题描述 投票:0回答:6

我正在尝试从 jstree 中获取选定的节点。

这是视图中的代码

<div id="divtree" >
    <ul id="tree" >
         @foreach (var m in Model.presidentList)
         {
             <li class="jstree-clicked">
                  <a href="#" class="usr">@m.Name</a>
                  @Html.Partial("Childrens", m)
              </li>
         }
     </ul>
</div>

这是我尝试检索节点名称的 javascript 部分

$(".jstree-clicked").click(function (e) {
        var node = $(this).jstree('get_selected').text();
        alert(node);
});

我在仅获取所选节点时遇到问题。如果我选择其中一个子节点(例如树的最后一个节点),我仍然可以获得整个节点列表。如果您知道我做错了什么,请告诉我?

javascript jquery razor jstree
6个回答
17
投票

我认为您不应该为每个

  • 节点分配类“jstree-clicked”。 并使用用于 jstree 绑定的 jstree 容器获取选定的节点。

    console.log($("#divtree").jstree("get_selected").text());
    

    var data = $(yourtree).jstree().get_selected(true)[0].text;

    console.log(data);
    

    这对我有用。尝试一下吧!

    由于更多人发现我的评论有用,我将其转换为答案。感谢 Murali 的回答,我能够解决我的问题。此代码:

    $("#divtree").jstree("get_selected",true)
    

    将返回完整的对象。 (看真实参数)

    试试这个:

    var CurrentNode = $("#divtree").jstree("get_selected");
    console.log($('#'+CurrentNode).text());
    

    这是我如何在他的“选定”父节点中创建一个新节点:

    <div class="input-group mb-3">
        <div class="input-group-prepend">
            <button class="btn btn-secondary" id="add_node">
                Add directory    
            </button>
        </div>
        <input type="text" class="form-control" placeholder="Dir name" id="node_text">
    </div>
    

    还有我的 JavaScript :

        htmlSource.jstree();
    
        $('#add_node').on('click', function () {
            let text = $(this).closest('.input-group').find('#node_text').val();
            let selected = htmlSource.jstree('get_selected');
            htmlSource.jstree().create_node(selected, {
                "text": text
            },
            "last",
            function (e) {
                console.log(e);
            });
        })
    

    我希望它有用或给某人带来指导

    $("#divtree").jsTree().get_selected(true) 将返回“节点”的数组


  • 13
    投票

    var data = $(yourtree).jstree().get_selected(true)[0].text;

    console.log(data);
    

    这对我有用。尝试一下吧!


    7
    投票

    由于更多人发现我的评论有用,我将其转换为答案。感谢 Murali 的回答,我能够解决我的问题。此代码:

    $("#divtree").jstree("get_selected",true)
    

    将返回完整的对象。 (看真实参数)


    3
    投票

    试试这个:

    var CurrentNode = $("#divtree").jstree("get_selected");
    console.log($('#'+CurrentNode).text());
    

    0
    投票

    这是我如何在他的“选定”父节点中创建一个新节点:

    <div class="input-group mb-3">
        <div class="input-group-prepend">
            <button class="btn btn-secondary" id="add_node">
                Add directory    
            </button>
        </div>
        <input type="text" class="form-control" placeholder="Dir name" id="node_text">
    </div>
    

    还有我的 JavaScript :

        htmlSource.jstree();
    
        $('#add_node').on('click', function () {
            let text = $(this).closest('.input-group').find('#node_text').val();
            let selected = htmlSource.jstree('get_selected');
            htmlSource.jstree().create_node(selected, {
                "text": text
            },
            "last",
            function (e) {
                console.log(e);
            });
        })
    

    我希望它有用或给某人带来指导


    0
    投票

    $("#divtree").jsTree().get_selected(true) 将返回“节点”的数组

    © www.soinside.com 2019 - 2024. All rights reserved.