I'm wondering if I can provide a checkbox only on the parent that has children with no children.
Something like this. I can select a single child, or the child's immediate parent that would select all the children.
parent01
parent02
parent03[]
child01[]
child02[]
child03[]
parent04[]
child04[]
parent05[]
child05[]
parent06
parent07
parent08[]
child06[]
child07[]
Here is a working example that follows the idea of my previous answer - setting the visibility of parents that have children with children to hidden.
To prevent hidden checkboxes from being checked by clicking the node, the checkbox plugin properties whole_node and tie_selection should be set to false.
The following code is reading the tree data and checking for each tree node, if the parent node has a parent node - let's call it a grandparent node. If so, the css visibility property of the grandparent node's checkbox is set to hidden.
var to_hide = []; // ids of nodes that won't have checkboxes
/* get the tree data and find the nodes that have
children with children
*/
function getGrandparents() {
// tree data
var item = $('#data').jstree(true).get_json('#', {flat:true});
console.log(JSON.stringify(item, undefined, 2));
var parent, grandparent;
// var nodeIds = item.map(x => x.id); // ids of nodes in object item
var nodeIds = item.map(function(x){return x.id;}); // ids of nodes in object item
for (var i = 0; i < item.length; i += 1) {
if (has_parent(item[i])) {
parent = item[nodeIds.indexOf(item[i].parent)];
if (has_parent(parent)) {
grandparent = parent.parent + '_anchor'; // node with id grandparent is parent of parent
// and therefore shouldn't have a checkbox
if (to_hide.indexOf(grandparent) < 0) {
to_hide[to_hide.length] = grandparent; // load id into array of checkboxes that will
// be hidden
}
}
}
}
function has_parent(who) {
return ((typeof who.parent !== 'undefined') && (who.parent !== '#'));
}
}
function hideGrandparents() {
var gpa;
// set visibility of checkbox nodes in to_hide to hidden
for (var n = 0; n < to_hide.length; n += 1) {
gpa = document.getElementById(to_hide[n]);
if (gpa != null ) {
gpa.getElementsByClassName('jstree-checkbox')[0].style.visibility = 'hidden';
}
}
}
$('#data').jstree({
'core' : {
'data' : [
{ "text" : "Root node",
"children" : [
{ "text" : "Child node 1 1",
"children" : [
{ "text" : "Child node 1 1 1" },
{ "text" : "Child node 1 1 2" }
]
},
{ "text" : "Child node 1 2",
"children" : [
{ "text" : "Child node 1 2 1",
"children" : [
{ "text" : "Child node 1 2 1 1" },
{ "text" : "Child node 1 2 1 2" }
]
},
{ "text" : "Child node 1 2 2" }
]
}
]
},
{ "text" : "Root node 2",
"children" : [
{ "text" : "Child node 2 1" },
]
}
]
},
'checkbox': {
three_state: false,
whole_node: false, // should be set to false. otherwise checking the hidden checkbox
// could be possible by clicking the node
tie_selection: false, // necessary for whole_node to work
cascade: 'up down'
},
'plugins': ["checkbox"]
}).on("ready.jstree", function() {
getGrandparents();
hideGrandparents(); // hide checkboxes of root nodes that have children with children
}).on("open_node.jstree", hideGrandparents);
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.1.1/themes/default/style.min.css" rel="stylesheet"/>
<div id="data"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.7/jstree.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With