Search code examples
javascriptjqueryjstree

How to get the ancestors of a JS tree node when it is selected?


I am getting the child tree name but I want to get its complete hierarchy of parent node names.

Below code shows how I get the child node and print its value in a particular div element:

$(document).ready(function () {
    $('#bdeViewNew').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.trim());
            $('#treeBreadCrumbs').html(r.join(', '));
        }
    });
});

Now it prints the value of child node, e.g. Child a. But I want something like follows, if the tree structure is as shown below:

Parent
  Child 1
    Child a
    Child b
  Child 2
    Child c
    Child d

so if I click on Child a I want my div content updated as

Parent > Child 1 > Child a

as of now I am getting Child a only. Please let me know how I can get the correct output.

I tried like this as shown below to get path of all the parent node:

$(document).ready(function() {
$('#bdeViewNew').on('changed.jstree', function(e, data) {
var ids = data.inst.get_path('#bdeViewNew' + data.rslt.obj.attr('id'),true);
var names = data.inst.get_path('#bdeViewNew' + data.rslt.obj.attr('id'),false);
alert("Path [ID or Name] from root node to selected node = ID's = "+ids+" :: Name's = "+names);
 });
 });

but still no result to get_path. Do I need to use different JS or a plugin? And what is the meaning of attr('id') i should pass the id of that li or something else as i did'nt understand this syntax properly.

Adding my jstree:

 <div id="bdeViewNew">
    <ul>


        <li id="bde" data-jstree='{"opened":true,"icon":"./images/tree.png"}'">
            Parent 1

            <ul>

                <li id="aaa" data-jstree='{"opened":true,"icon":"./images/tree.png"}'>
                    Child 1 <c:forEach items="${empInfo.empList}"
                        var="empValue">
                        <ul>
                            <li id="bbb" data-jstree='{"icon":"./images/tree.png"}' >${empValue.empName}</li>
                        </ul>
                    </c:forEach>
                </li>
            </ul>
            <ul>
                <li id="ccc" data-jstree='{"icon":"./images/tree.png"}'>Child 2
                    <c:forEach items="${imgInfo.imgList}"
                        var="imgValue">
                        <ul>
                            <li id="ddd" data-jstree='{"icon":"./images/tree.png"}'>${imgValue.imgName}</li>
                        </ul>
                    </c:forEach>
                </li>
            </ul>
        </li>
    </ul>
</div>

Solution

  • This will work fine... it will get the full parents...

        $('#bdeViewNew').on('select_node.jstree', function (e, data) {
            var loMainSelected = data;
            uiGetParents(loMainSelected);
        });
    
        function uiGetParents(loSelectedNode) {
            try {
                var lnLevel = loSelectedNode.node.parents.length;
                var lsSelectedID = loSelectedNode.node.id;
                var loParent = $("#" + lsSelectedID);
                var lsParents =  loSelectedNode.node.text + ' >';
                for (var ln = 0; ln <= lnLevel -1 ; ln++) {
                    var loParent = loParent.parent().parent();
                    if (loParent.children()[1] != undefined) {
                        lsParents += loParent.children()[1].text + " > ";
                    }
                }
                if (lsParents.length > 0) {
                    lsParents = lsParents.substring(0, lsParents.length - 1);
                }
                alert(lsParents);
            }
            catch (err) {
                alert('Error in uiGetParents');
            }
        }