jsTree - is_parent() returning wrong value












1















I'm requesting data from a remote server with json return in format :



[
{"id":"1", "parent": "#", "text" : "Parent1"},
{"id":"2", "parent": 1, "text" : "Child1"}
{"id":"3", "parent": 2, "text" : "Child12"}
{"id":"4", "parent": 1, "text" : "Child2"}
{"id":"5", "parent": 1, "text" : "Child3"}
{"id":"6", "parent": 4, "text" : "Child21"}
]


I would like to check if the selected node is a parent. I use this code:



$('#treeview').on("select_node.jstree", function (e, data) {
var isParent = data.instance.is_parent();
alert(isParent)
});


It always returns false even when I click on PARENT.



What am I missing here ?



UPDATE
This is how I get solved the issue. But I still wonder why the methods is_parent() and is_leaf() are not working



var isParent = (data.node.children.length > 0);









share|improve this question

























  • see stackoverflow.com/questions/30782213/…

    – Tamil Selvan C
    Nov 23 '18 at 7:00











  • @TamilSelvanC I checked the link before but using var isParent = data.instance.is_parent(); alert(isParent) does not solve the problem

    – Kwaye Kant
    Nov 23 '18 at 7:13











  • Hi, i updated my answer. Please check

    – Tamil Selvan C
    Nov 23 '18 at 7:28













  • I got it. Thanks

    – Kwaye Kant
    Nov 23 '18 at 8:13
















1















I'm requesting data from a remote server with json return in format :



[
{"id":"1", "parent": "#", "text" : "Parent1"},
{"id":"2", "parent": 1, "text" : "Child1"}
{"id":"3", "parent": 2, "text" : "Child12"}
{"id":"4", "parent": 1, "text" : "Child2"}
{"id":"5", "parent": 1, "text" : "Child3"}
{"id":"6", "parent": 4, "text" : "Child21"}
]


I would like to check if the selected node is a parent. I use this code:



$('#treeview').on("select_node.jstree", function (e, data) {
var isParent = data.instance.is_parent();
alert(isParent)
});


It always returns false even when I click on PARENT.



What am I missing here ?



UPDATE
This is how I get solved the issue. But I still wonder why the methods is_parent() and is_leaf() are not working



var isParent = (data.node.children.length > 0);









share|improve this question

























  • see stackoverflow.com/questions/30782213/…

    – Tamil Selvan C
    Nov 23 '18 at 7:00











  • @TamilSelvanC I checked the link before but using var isParent = data.instance.is_parent(); alert(isParent) does not solve the problem

    – Kwaye Kant
    Nov 23 '18 at 7:13











  • Hi, i updated my answer. Please check

    – Tamil Selvan C
    Nov 23 '18 at 7:28













  • I got it. Thanks

    – Kwaye Kant
    Nov 23 '18 at 8:13














1












1








1








I'm requesting data from a remote server with json return in format :



[
{"id":"1", "parent": "#", "text" : "Parent1"},
{"id":"2", "parent": 1, "text" : "Child1"}
{"id":"3", "parent": 2, "text" : "Child12"}
{"id":"4", "parent": 1, "text" : "Child2"}
{"id":"5", "parent": 1, "text" : "Child3"}
{"id":"6", "parent": 4, "text" : "Child21"}
]


I would like to check if the selected node is a parent. I use this code:



$('#treeview').on("select_node.jstree", function (e, data) {
var isParent = data.instance.is_parent();
alert(isParent)
});


It always returns false even when I click on PARENT.



What am I missing here ?



UPDATE
This is how I get solved the issue. But I still wonder why the methods is_parent() and is_leaf() are not working



var isParent = (data.node.children.length > 0);









share|improve this question
















I'm requesting data from a remote server with json return in format :



[
{"id":"1", "parent": "#", "text" : "Parent1"},
{"id":"2", "parent": 1, "text" : "Child1"}
{"id":"3", "parent": 2, "text" : "Child12"}
{"id":"4", "parent": 1, "text" : "Child2"}
{"id":"5", "parent": 1, "text" : "Child3"}
{"id":"6", "parent": 4, "text" : "Child21"}
]


I would like to check if the selected node is a parent. I use this code:



$('#treeview').on("select_node.jstree", function (e, data) {
var isParent = data.instance.is_parent();
alert(isParent)
});


It always returns false even when I click on PARENT.



What am I missing here ?



UPDATE
This is how I get solved the issue. But I still wonder why the methods is_parent() and is_leaf() are not working



var isParent = (data.node.children.length > 0);






javascript json jstree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 8:12







Kwaye Kant

















asked Nov 23 '18 at 6:33









Kwaye KantKwaye Kant

3616




3616













  • see stackoverflow.com/questions/30782213/…

    – Tamil Selvan C
    Nov 23 '18 at 7:00











  • @TamilSelvanC I checked the link before but using var isParent = data.instance.is_parent(); alert(isParent) does not solve the problem

    – Kwaye Kant
    Nov 23 '18 at 7:13











  • Hi, i updated my answer. Please check

    – Tamil Selvan C
    Nov 23 '18 at 7:28













  • I got it. Thanks

    – Kwaye Kant
    Nov 23 '18 at 8:13



















  • see stackoverflow.com/questions/30782213/…

    – Tamil Selvan C
    Nov 23 '18 at 7:00











  • @TamilSelvanC I checked the link before but using var isParent = data.instance.is_parent(); alert(isParent) does not solve the problem

    – Kwaye Kant
    Nov 23 '18 at 7:13











  • Hi, i updated my answer. Please check

    – Tamil Selvan C
    Nov 23 '18 at 7:28













  • I got it. Thanks

    – Kwaye Kant
    Nov 23 '18 at 8:13

















see stackoverflow.com/questions/30782213/…

– Tamil Selvan C
Nov 23 '18 at 7:00





see stackoverflow.com/questions/30782213/…

– Tamil Selvan C
Nov 23 '18 at 7:00













@TamilSelvanC I checked the link before but using var isParent = data.instance.is_parent(); alert(isParent) does not solve the problem

– Kwaye Kant
Nov 23 '18 at 7:13





@TamilSelvanC I checked the link before but using var isParent = data.instance.is_parent(); alert(isParent) does not solve the problem

– Kwaye Kant
Nov 23 '18 at 7:13













Hi, i updated my answer. Please check

– Tamil Selvan C
Nov 23 '18 at 7:28







Hi, i updated my answer. Please check

– Tamil Selvan C
Nov 23 '18 at 7:28















I got it. Thanks

– Kwaye Kant
Nov 23 '18 at 8:13





I got it. Thanks

– Kwaye Kant
Nov 23 '18 at 8:13












1 Answer
1






active

oldest

votes


















1














To get parent



use



var isParent = (data.node.children.length > 0);
alert(isParent );





$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>








share|improve this answer


























  • Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

    – Kwaye Kant
    Nov 23 '18 at 7:10








  • 1





    try var isParent = (data.node.children.length > 0); alert(isParent );

    – Tamil Selvan C
    Nov 23 '18 at 7:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441642%2fjstree-is-parent-returning-wrong-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














To get parent



use



var isParent = (data.node.children.length > 0);
alert(isParent );





$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>








share|improve this answer


























  • Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

    – Kwaye Kant
    Nov 23 '18 at 7:10








  • 1





    try var isParent = (data.node.children.length > 0); alert(isParent );

    – Tamil Selvan C
    Nov 23 '18 at 7:29
















1














To get parent



use



var isParent = (data.node.children.length > 0);
alert(isParent );





$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>








share|improve this answer


























  • Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

    – Kwaye Kant
    Nov 23 '18 at 7:10








  • 1





    try var isParent = (data.node.children.length > 0); alert(isParent );

    – Tamil Selvan C
    Nov 23 '18 at 7:29














1












1








1







To get parent



use



var isParent = (data.node.children.length > 0);
alert(isParent );





$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>








share|improve this answer















To get parent



use



var isParent = (data.node.children.length > 0);
alert(isParent );





$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>








$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>





$('#treeview').jstree({
'core': {
'data': [{
"id": "1",
"parent": "#",
"text": "Parent1"
}, {
"id": "2",
"parent": 1,
"text": "Child1"
},
{
"id": "21",
"parent": 2,
"text": "Child1"
},
{
"id": "3",
"parent": 2,
"text": "Child12"
}, {
"id": "4",
"parent": 1,
"text": "Child2"
}, {
"id": "5",
"parent": 1,
"text": "Child3"
},
{
"id": "6",
"parent": 4,
"text": "Child21"
},
{
"id": "7",
"parent": '#',
"text": "Parent 2"
},
{
"id": "8",
"parent": 7,
"text": "Child"
}
]
}
});

$('#treeview').on("select_node.jstree", function(e, data) {
// var isParent = data.instance.is_parent(data);
// If you need to check if a node is a root node you can use:
var isParent = (data.node.children.length > 0);
console.log(data.node);
alert(isParent)
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>

<div id="treeview"></div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 7:28

























answered Nov 23 '18 at 6:52









Tamil Selvan CTamil Selvan C

14.5k93659




14.5k93659













  • Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

    – Kwaye Kant
    Nov 23 '18 at 7:10








  • 1





    try var isParent = (data.node.children.length > 0); alert(isParent );

    – Tamil Selvan C
    Nov 23 '18 at 7:29



















  • Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

    – Kwaye Kant
    Nov 23 '18 at 7:10








  • 1





    try var isParent = (data.node.children.length > 0); alert(isParent );

    – Tamil Selvan C
    Nov 23 '18 at 7:29

















Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

– Kwaye Kant
Nov 23 '18 at 7:10







Thank you for your reply @Tamil but this does not solve the problem. It only works on root nodes.For any node in the tree I want to know if it is a parent and then get all it's childreen

– Kwaye Kant
Nov 23 '18 at 7:10






1




1





try var isParent = (data.node.children.length > 0); alert(isParent );

– Tamil Selvan C
Nov 23 '18 at 7:29





try var isParent = (data.node.children.length > 0); alert(isParent );

– Tamil Selvan C
Nov 23 '18 at 7:29




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441642%2fjstree-is-parent-returning-wrong-value%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy