繁体   English   中英

从 jstree 获取选定的节点

[英]Get the selected Node from a jstree

我正在尝试从 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);
});

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

我不认为您应该为每个<li>节点分配类“ jstree-clicked”。 并使用用于jstree绑定的jstree容器获取选定的节点。

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

由于更多的人认为我的评论有用,因此将其转换为答案。 感谢Murali的回答,我得以解决我的问题。 这段代码:

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

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

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

console.log(data);

这对我有用。 试一试!

尝试这个:

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

我希望它可以有用或给某人一个领导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM