How to get selected node's text in jstree

依然范特西╮ 提交于 2019-12-11 02:24:27

问题


I have a jstree like shown below

and have the code in my script

$("#jstree").click(function (e) {
    var node = $("#jstree").jstree('get_selected');
    $('#resultText').html(node);
});

What i need is if i am selecting book, title, price i need to display output as book-title-price. By using above code i can get ids ie. 1-1_1-1_2 of my selected nodes but how can i get texts of the selected nodes in the above format?

COMPLETE CODE for jstree integration.

@model List<XMLNodeSearch.Models.NodeSearchModel>
<link href="~/vakata-jstree-8ea6ce7/dist/themes/default/style.min.css"     rel="stylesheet" />
<script src="~/Script/jquery-1.12.0.min.js"></script>
<script src="~/vakata-jstree-8ea6ce7/dist/jstree.min.js"></script>
<script src="~/vakata-jstree-8ea6ce7/dist/jstree.js"></script>

<script>
   $(function () {
   $('#jstree').jstree();
   $("#jstree").click(function (e) {
   var node = $("#jstree").jstree('get_selected').text();//This line with text() will throw an error
   $('#resultText').html(node);
  });
</script>

<section>
<label>Result - <span id="resultText"></span></label>
<div id="NodeResult">
    @if (Model != null)
    { 
        <div id="jstree">
            @foreach (var treeNode in Model)
            {
                <!-- in this example the tree is populated from inline HTML -->
                <ul>
                    <li id="@treeNode.Id">@treeNode.NodeName
                        @if(treeNode.ChildNode.Count() > 0)
                        {
                            <ul>
                                @foreach (var childNd in treeNode.ChildNode)
                                {
                                    <li id="@childNd.Id">@childNd.NodeName</li>
                                }
                            </ul>
                        }
                    </li>
                </ul>
            }
        </div>
    }
</div>
</section>

回答1:


Answer is a bit late, but thought will post anyways.

This will only get the id.

$('#tree_container').jstree('get_selected')

This should get the text.

$('#tree_container').jstree('get_selected', true)[0].text;



回答2:


Finally found the solution to my problem. I am posting the answer here for anyone's future reference.

What i did is added the following script in my script block.

    $('#jstree').on('changed.jstree', function (e, data) {
        var i, j, r = [];
        for (i = 0, j = data.selected.length; i < j; i++) {
            r.push(data.instance.get_node(data.selected[i]).text);
        }
        $('#resultText').html('Selected: ' + r.join(', '));
    }).jstree();

This given me the desired output.




回答3:


You can try using this way, if it works for you.

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



来源:https://stackoverflow.com/questions/35170283/how-to-get-selected-nodes-text-in-jstree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!