Hi Guys,
I am trying to save the sort order to my database but am having trouble with shifting children up and down depending on where you drop the node.
I have the bind move_node working now and also my Ajax is working but cannot get the logic behind the shifting nodes up and down right.
I have been trying to do everything in one main query using a case in the SQL.
.bind("move_node.jstree", function (e, data) {
console.log(data);
/*
data.rslt contains:
.o - the node being moved
.r - the reference node in the move
.ot - the origin tree instance
.rt - the reference tree instance
.p - the position to move to (may be a string - "last", "first", etc)
.cp - the calculated position to move to (always a number)
.np - the new parent
.oc - the original node (if there was a copy)
.cy - boolen indicating if the move was a copy
.cr - same as np, but if a root node is created this is -1
.op - the former parent
.or - the node that was previously in the position of the moved node
*/
$.ajax({
"type": "POST",
"dataType": "JSON",
"url": "./ajax/controller.php",
"data": { "action" : "move", "id" :
data.node.id, "parent" : data.parent, "pos" : data.position },
"success": function (data) {
console.log(data);
}
});
if(array_key_exists("action",$_REQUEST) && $_REQUEST['action'] == "move") {
$get_max_sql = "SELECT max(pt_order) as maximum FROM pagetext WHERE parent_id = " . $parent . " ORDER by pt_order";
$result = $mysqli->query($get_max_sql);
$max = $result->fetch_array(MYSQLI_ASSOC);
// Get all children within parent apart from the moved node
$get_children_sql = "SELECT pt_id, pt_order as pos FROM pagetext WHERE parent_id = " . $parent . " AND pt_id != " . $node_id . " ORDER by pt_order";
$result = $mysqli->query($get_children_sql);
// An array containing the category ids as keys and the new positions as values
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
//Decide on the order
if($row['pos'] < $order) {
$difference = -1;
} elseif($row['pos'] >= $order) {
$difference = 1;
}
//Lower Bounds
if($row['pos'] + $difference < 0) {
$display_order[$row['pt_id']] = 0;
//Upper Bounds
} elseif($row['pos'] + $difference >= $max['maximum']) {
$display_order[$row['pt_id']] = $max['maximum'];
//Normal Case
} else {
$display_order[$row['pt_id']] = ($row['pos'] + $difference);
}
}
$ids = implode(',', array_keys($display_order));
$reorder_sql = "UPDATE pagetext SET pt_order = CASE pt_id ";
foreach ($display_order as $id => $ordinal) {
$reorder_sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal);
}
$reorder_sql .= "END WHERE pt_id IN ($ids)";
#$result = $mysqli->query($reorder_sql);
// Set the moved element to within the parent at the current $order;
$move_sql = "UPDATE pagetext SET pt_order = " . $order . " WHERE parent_id = " . $parent . " AND pt_id = " . $node_id;
#$result = $mysqli->query($move_sql);
$debug[] = $display_order;
$debug[] = $move_sql;
$debug[] = $get_children_sql;
$debug[] = $reorder_sql;
echo json_encode($debug);
}
I would appreciate anyones help. Ivan you are a superstar and I know you'll be the boss with this. As you can see I have given it a go myself taken me all day.
But I'm not as smart as you guys.
Regards,
Mike