JSTree check selected by leaf nodes or makes only leaves selectable

I created jstree as follows

$('#js-tree').jstree({ 'core' : { 'data' : { 'url' : "${pageContext.request.contextPath}/makeTree", "plugins" : [ "types", "search"], 'data' : function (node) { return { 'id' : node.id }; } } } }); 

I want to check if a node leaf is selected node when sending information -

 var selectedLeaves = $("#js-tree").jstree("get_selected"); 

But this only gives me an id array. How to use the is_leaf method to filter our only leaf nodes from selected nodes? I referred to this post - Force the user to select only leaf nodes. This solution did not work for me.

+3
source share
2 answers

I found the solution myself, using the get_selected api argument to filter leaves from folders -

 var nodes = $("#js-tree").jstree(true).get_selected("full", true); $.each(nodes,function(i, node){ if(node.children.length >0){ console.log("not leaf"); } else{ console.log("leaf"); } }); 

If you have a better solution, let me know.

+3
source

Here's what worked for me

 var res = jQuery.jstree.reference('#js-tree').get_json('#', { flat: true }); var cnt = 0; for (var i = 0, j = res.length; i < j; i++) { if (res[i].parent !== '#') { cnt++; } } 

cnt is our team

0
source

Source: https://habr.com/ru/post/1386288/


All Articles