$('#exportbutton').on('click', function() {
// get the ids of the selected nodes. // false causes only the ID to be returned
var selectedNodes = $('#myjstree').jstree('get_selected', false);
var selectedAndUndetermined = [];
for(i = 0; i < selectedNodes.length; i++) {
// collect the ids of the selected nodes along with their parents
selectedAndUndetermined = selectedAndUndetermined.concat($('#myjstree').jstree('get_path', selectedNodes[i], false, true));
}
// remove duplicates (i.e. create an array with unique elements)
selectedAndUndetermined = Array.from(new Set(selectedAndUndetermined));
// create the selected subtree (the combination of selected and undetermined nodes)
var subtree = [];
for(j = 0; j < selectedAndUndetermined.length; j++) {
nodei = $('#myjstree').jstree('get_node', selectedAndUndetermined[j], false);
subtree = subtree.concat(nodei);
}
console.log(subtree);
In my case, however, the json data is in the (id, parent) format (i.e. no node.children element).
$('#exportbutton').on('click', function() { // get the ids of the selected nodes. // false causes only the ID to be returned var selectedNodes = $('#myjstree').jstree('get_selected', false);
var undeterminedNodes = $('#myjstree').jstree('get_undetermined', false); var selectedAndUndetermined = selectedNodes.concat(undeterminedNodes); console.log('selected and undetermined', selectedAndUndetermined);
var jsonFullTree = $('#myjstree').jstree('get_json', '#', { 'flat' : true });
const subTree = jsonFullTree.filter((mynode) => selectedAndUndetermined.includes(mynode.id)); console.log('subtree', subTree);