how to get complete parent node name in js tree while selecting the child node

北战南征 提交于 2020-01-04 04:39:08

问题


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>

回答1:


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



回答2:


var node=$('#drives_tree').jstree("get_selected", true);
$("#breadcrumbs").text($('#drives_tree').jstree().get_path(node[0], ' > '));



回答3:


We have direct method to get the parent information.

  $('#bdeViewNew').on('select_node.jstree', function (e, data) {
        var loMainSelected = data;
        alert(loMainSelected.node.parents);
    });

It will return ["Child1", "Parent"] . Using this method we can get the all the parents up to root.




回答4:


function uiGetParents(loSelectedNode) {
    try {
        var loData = [];
        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 + " > ";
                loData.push(loParent.children()[1].text);
            }
        }
        if (lsParents.length > 0) {
            lsParents = lsParents.substring(0, lsParents.length - 1);
        }
        alert(lsParents);
        alert(loData.reverse());
    }
    catch (err) {
        alert('Error in uiGetParents');
    }
}

The result stored in array loData . and just reverse the data and loop the loData Array. You got u r output




回答5:


**

Step 1 : Get the Selected node Id and selected node level
        Step 2 : Using node id get the current parent like this ( ex.. $("#101000000892").parent().parent() )
            step 2.2 : Next level node ( $("#101000000892").parent().parent().parent().parent() )
            step 2.3 : stroe this in a vairable  var loNode = $("#101000000892").parent().parent();
            step 2.4 : Next you can  loNode.parent().parent()
        Step 3 : Using for loop u can loop through the level until reached 1.
        step 4 : Now you can get the full text .

**




回答6:


Try this,

Instead of angular.each, use a for loop.

You will get an arry with all the text of the nodes.

var breadcrumbArr = [selectedNode.node.text];

angular.forEach(selectedNode.node.parents, function(element, index) {
    if (element === '#') return;
    breadcrumbArr.push($('#'+element).find('a:first').text())
});

breadcrumbArr.reverse();

return breadcrumbArr;



回答7:


A slightly less verbose version that prints Parent > Child

function uiGetParents(node) {
try {
    var level = node.node.parents.length;
    var elem = $('#' + node.node.id);
    var text = node.node.text;

    for (var ln = 0; ln <= level - 1 ; ln++) {
        elem = elem.parent().parent();

        var child = elem.children()[1];
        if (child != undefined) {
            text = child.text + ' > ' + text;
        }
    }

    console.log(text);
}
catch (err) {
    console.log('Error in uiGetParents');
}

}




回答8:


var node = $tree.jstree().get_node(id),
    formatted_name = $tree.jstree().get_text(node);

    $.each(node.parents, function(key, parentId) {
        if ( parentId !== "#" ) {
            var parent = $tree.jstree().get_node(parentId);
            formatted_name = $tree.jstree().get_text(parent) + "->" + formatted_name;
        }
    });

   console.log(formatted_name);


来源:https://stackoverflow.com/questions/27169820/how-to-get-complete-parent-node-name-in-js-tree-while-selecting-the-child-node

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