var jstEdit;
$('#types-list-edit').on('move_node.jstree', function(e, jst) { console.log("DROPPED", jst); updatePositions(jst);}).jstree({ "checkbox" : { "keep_selected_style" : false } ,"core" : { 'data' : { 'url' : '/api/getTree', 'data' : function (node) { return { 'id' : node.id }; } } ,'check_callback' : true } ,"plugins" : [ "contextmenu" ,"unique" ,"dnd" ,"sort" ,"wholerow" ] ,'contextmenu' : { 'select_node' : false } ,'sort' : function(node1, node2) {return sortNodes("edit", node1, node2)}});
jstEdit = $.jstree.reference('#types-list-edit');
// other stuff here, not related to this
function updatePositions(jst) { var updates = []; var parents = []; // always add the current parent parents.push(jst.parent); // add old_parent to parents array if this move changed parents for the node if (jst.parent != jst.old_parent) { parents.push(jst.old_parent); } // loop thru the parents for (y = 0; y < parents.length; y++) { // get the array of children ids from the parent if (parents[y] == '#') { var nodes = []; $('#types-list-edit ul.jstree-container-ul li').each(function(){ // console.log("ROOT ID", $(this).attr('id')); nodes.push($(this).attr('id')); }); } else { var nodes = jstEdit.get_node(parents[y]).children; } // loop thru the nodes ids array for (x = 0; x < nodes.length; x++) { var node = jstEdit.get_node(nodes[x]); // update the position, only if it's different if (node.data.position != x) { node.data.position = x; // update parent to be an int for DB PK if (node.parent == "#") { node.parent = 0; } // build the object & add to updates array updates.push({ id : node.id ,parent : node.parent ,position : node.data.position }); } } }} [ { a_attr : "", icon : "", id : 6, li_attr : "", parent : 5, text : "P2", data : { position : 5 } }, { a_attr : "", icon : "", id : 36, li_attr : "", parent : 46, text : "36", data : { position : 0 } }, { a_attr : "", icon : "", id : 37, li_attr : "", parent : "#", text : "P5", data : { position : 4 } }, { a_attr : "", icon : "", id : 38, li_attr : "", parent : "#", text : "P6", data : { position : 5 } }, { a_attr : "", icon : "", id : 39, li_attr : "", parent : 46, text : "39", data : { position : 0 } }, { a_attr : "", icon : "", id : 40, li_attr : "", parent : 46, text : "40", data : { position : 0 } }, { a_attr : "", icon : "", id : 41, li_attr : "", parent : "#", text : "P3", data : { position : 2 } }, { a_attr : "", icon : "", id : 42, li_attr : "", parent : 6, text : "C5", data : { position : 0 } }, { a_attr : "", icon : "", id : 43, li_attr : "", parent : 46, text : "43", data : { position : 0 } }, { a_attr : "", icon : "", id : 45, li_attr : "", parent : 41, text : "C6", data : { position : 0 } }, { a_attr : "", icon : "", id : 46, li_attr : "", parent : "#", text : "P1", data : { position : 0 } }, { a_attr : "", icon : "", id : 47, li_attr : "", parent : "#", text : "P4", data : { position : 3 } }, { a_attr : "", icon : "", id : 48, li_attr : "", parent : 46, text : "48", data : { position : 0 } }, { a_attr : "", icon : "", id : 49, li_attr : "", parent : 46, text : "49", data : { position : 0 } }]
Uncaught TypeError: Cannot read property 'children' of undefined jstree.js:1708$.jstree.core.redraw_node jstree.js:1708$.jstree.plugins.wholerow.redraw_node jstree.js:6013$.jstree.core._redraw jstree.js:1590$.jstree.core.redraw jstree.js:1623$.jstree.core.redraw_node jstree.js:1647$.jstree.plugins.wholerow.redraw_node jstree.js:6013(anonymous function) jstree.js:5552n.extend.proxy.n.isFunction.e jquery.js:2n.event.dispatch jquery.js:3n.event.add.r.handle jquery.js:3n.event.trigger jquery.js:3n.fn.extend.triggerHandler jquery.js:3$.jstree.core.trigger jstree.js:693$.jstree.core.move_node jstree.js:3180$.jstree.core.move_node jstree.js:3052(anonymous function) jstree.js:4898n.event.dispatch jquery.js:3n.event.add.r.handle jquery.js:3n.event.trigger jquery.js:3n.fn.extend.triggerHandler jquery.js:3$.vakata.dnd._trigger jstree.js:4946$.vakata.dnd.stop jstree.js:5152n.event.dispatch jquery.js:3n.event.add.r.handle jquery.js:3
function sortNodes(jstInstance, node1, node2) { if (jstInstance == "associate") { var a = jstAssoc.get_node(node1); var b = jstAssoc.get_node(node2); } else { var a = jstEdit.get_node(node1); var b = jstEdit.get_node(node2); } var a_pos = a.data.position; var a_name = a.text; var b_pos = b.data.position; var b_name = b.text; // sort by position if position values are present if (parseInt(a_pos) > -1 && parseInt(b_pos) > -1) { if (parseInt(a_pos) > parseInt(b_pos)) { return 1; } else { return -1; } } else { // if no position values, sort alphabetical if (a_name > b_name) { return 1; } else { return -1; } }}function updatePositions(e, jst) { var updates = []; var parents = []; // always add the current parent parents.push(jst.parent); // add old_parent to parents array if this move changed parents for the node if (jst.parent != jst.old_parent) { parents.push(jst.old_parent); } // loop thru the parents for (y = 0; y < parents.length; y++) { // get the array of children ids from the parent var nodes = jstEdit.get_node(parents[y]).children; // loop thru the nodes ids array for (x = 0; x < nodes.length; x++) { var node = jstEdit.get_node(nodes[x]), position, parent; // update the position, only if it's different if (node.data.position != x) { position = x; // update parent to be an int for DB PK parent = (node.parent == "#") ? 0 : node.parent ,parent : parent ,position : position }); } } } // submit results $.ajax({ url : '/api/updateThemePositions' ,type : 'POST' ,data : {'themes' : updates} }) .done(function(response) { notifyUser('Theme positions modification(s).', response.success) });}