Uncheck Parent Node Only If All Children Are Unchecked In jsTree.js
Hello all,
I was confronted with a minor issue in jsTree.js on how to uncheck a parent only if all it's child notes are unchecked. I checked on the documentation if there's an existing property or attribute that will be set to achieve this functionality. Since there is none, I decided to just do the coding stuff to solve this problem. The logic behind this is that get the parent node and it's direct descendants. If there are child nodes, loop through those elements and if one of them is still checked, the parent node remains checked. But to make this work, the properties cascade up and three_state should be set as 'up' and 'false'.
I was confronted with a minor issue in jsTree.js on how to uncheck a parent only if all it's child notes are unchecked. I checked on the documentation if there's an existing property or attribute that will be set to achieve this functionality. Since there is none, I decided to just do the coding stuff to solve this problem. The logic behind this is that get the parent node and it's direct descendants. If there are child nodes, loop through those elements and if one of them is still checked, the parent node remains checked. But to make this work, the properties cascade up and three_state should be set as 'up' and 'false'.
$('#jsEmployees').jstree({ 'checkbox': { keep_selected_style: false, three_state: false, cascade: 'up' }, 'core': { 'themes': { 'icons': false } }, 'plugins': ["defaults", "checkbox"], 'expand_selected_onload': true }).bind('deselect_node.jstree', function (e, data) { if (data.event) { data.instance.deselect_node(data.node); //solution to uncheck parent node if there are no children selected var pNode = data.instance.get_node(data.node.parent); var children = data.instance.get_node(pNode).children; if (children.length > 0) { for (var i = 0; i < children.length; i++) { var result = data.instance.is_selected(children[i]); if (result) { data.instance.select_node(pNode); break; } } } } });
Comments
Post a Comment