Skip to content

Instantly share code, notes, and snippets.

@elidrissidev
Created August 2, 2020 10:10
Show Gist options
  • Save elidrissidev/fda9d19dfade32f94cb33e9768ca2523 to your computer and use it in GitHub Desktop.
Save elidrissidev/fda9d19dfade32f94cb33e9768ca2523 to your computer and use it in GitHub Desktop.
JsTree example with lazy loading and checked state cascading turned off
$(document).ready(function() {
$('.jstree').jstree({
core: {
data: {
url: function(node) {
return node.id === '#' ?
'categories.json' :
`categories-${node.id}.json`;
}
}
},
plugins: ['checkbox'],
checkbox: {
three_state: false // disable cascading checked state to all children nodes
}
});
var categories = [];
$('.jstree').on('loaded.jstree', function() {
$.ajax({
url: 'product_categories.json',
success: function(data) {
var categoriesToOpen = data.reduce(function(prev, curr) {
return prev.concat(curr.path.split('/'));
}, []);
categories = data.reduce(function(prev, curr) {
return prev.concat(curr.category_id);
}, []);
var set = new Set(categoriesToOpen);
$('.jstree').jstree('load_node', [...set]);
}
});
});
$('.jstree').on('load_node.jstree', function(_node, _status) {
$('.jstree').jstree('select_node', categories);
});
$('.get-selected').on('click', function() {
console.log($('.jstree').jstree('get_selected'));
});
});
@medgoprot
Copy link

Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment