JSTree Parent Node Text

Manoj Patil
Manoj Patil used Ask the Experts™
on
Hello Experts,
I am using JSTree plugin to display my Data.
But when I select the Child node, I wanted to get the Parent node Text on form submit.
I have tried below code.

$(document).on('click', '#btnSubmit', function () {
var parent = [];
        var selectedElms =$js('#IndustryTree').jstree("get_selected", true);
        $.each(selectedElms, function () {
            selectedElmsIds.push(this.text);
            //parent.push(this.parent);            
            var node = $js('#IndustryTree').jstree(true).get_node(this.parent, true);            
            var Parentnode = $js('#IndustryTree').find("[id='" + this.parent + "']");
            parent.push(Parentnode[0].innerText);
        });
});

Open in new window

But When I run this code, in Parentnode[0].innerText I am getting Parent Node text as well as all Child node text. So it is difficult to differentiate the Parent Node Text.

Another issue is when Checked the Parent node, then all child nodes are selected. But I am not getting that selected parent node using below code

var selectedElms =$js('#IndustryTree').jstree("get_selected", true);

Parent Node selection

This code is working when I particularly select the Child node, in this case, parent node CSS class will be jstree-icon jstree-checkbox jstree-undetermined

Child Node selection is Working
Any help would be appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
Can you post a piece of the HTML code to work more efficiently?
Manoj PatilSr. Software Engineer

Author

Commented:
Hi,
I am binding the values from server side in .NET MVC.
So there is just a div to generate the tree.
The main operation is in javascript
leakim971Multitechnician
Top Expert 2014
Commented:
Try this :
$(document).on('click', '#btnSubmit', function () {
        var parent = [];
        var selectedElms = $js('#IndustryTree').jstree("get_selected", true);
        for(var el=0;el<selectedElms.length;el++) {
               selectedElmsIds.push(selectedElms[el].text); // I'm assuming you declared selectedElmsIds somewhere...
               parent = parent.concat(selectedElms[el].parents);
        }
});

Open in new window

Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Manoj PatilSr. Software Engineer

Author

Commented:
Hi,
I got the Parent node now when I select the Child node instead of Parent Node.

Now, when I am selecting the Parent node directly, all the child node are selecting, But I am not getting the text values of parent as well as child nodes

How can I get it ?
Sr. Software Engineer
Commented:
I got the solution using below code. It might help someone. Using below code you will get parent node text as well as children node text


 
            var selectedElmsIds = [];
            var aPrimaryIndustaries = [];
            var aSecondaryIndustaries = [];            
            var selectedElms = $js('#IndustryTree').jstree("get_selected", true);
            $.each(selectedElms, function () {
                aSecondaryIndustaries.push(this.text);                
                SecondaryInd = SecondaryInd + this.text + "$";
                if ($js.inArray(this.parent.toString().trim(), aPrimaryIndustaries) == -1)
                    aPrimaryIndustaries.push(this.parent.trim());
            });

Open in new window

Manoj PatilSr. Software Engineer

Author

Commented:
The code posted by me is working for me.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial