Created
August 2, 2020 10:10
-
-
Save elidrissidev/fda9d19dfade32f94cb33e9768ca2523 to your computer and use it in GitHub Desktop.
JsTree example with lazy loading and checked state cascading turned off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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')); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your help!