fix: make drag-and-drop on category tree persist correctly

Two issues prevented the shelf tree reordering from working:

- The JS used ``onDragStop`` (only fires for drags outside the
  tree) instead of the ``tree.move`` event to send the AJAX
  request. Moved the POST into the ``tree.move`` handler.
- The ``inside`` case used ``appendNode`` (last child), but
  jqTree sends ``inside`` when dropping before the first child.
  Switched to ``prependNode`` so the node lands first.
- Added missing ``before`` case with ``insertBeforeNode``.
This commit is contained in:
Valentin Lab
2026-02-13 05:42:54 +01:00
parent 7a246a189a
commit cd5d72e272
2 changed files with 17 additions and 46 deletions

View File

@@ -14,57 +14,30 @@
@push('js')
<script>
var position = '';
var target_node = '';
$(function() {
var $tree = $('#tree1').tree({
dragAndDrop: true,
onDragStop: handleMove,
autoOpen: 0
});
$tree.on('tree.move', function(e) {
// e.preventDefault();
var position = e.move_info.position;
var target_node = e.move_info.target_node;
var moved_node = e.move_info.moved_node;
position = e.move_info.position;
target_node = e.move_info.target_node;
function getNewParentNode() {
if (position == 'inside') {
return target_node;
}
else {
// before or after
return target_node.parent;
}
}
var parent_node = getNewParentNode();
console.log("Parent node", parent_node);
$.ajax({
method: "POST",
url: "{{ route('Admin.Shop.Categories.moveTree') }}",
data: {
node_id: moved_node.id,
type: position,
target_id: target_node.id
}
});
});
});
function handleMove(node, e) {
console.log(node);
node_id = node.id;
console.log(node_id);
console.log(position);
console.log(target_node);
target_node_id = target_node.id;
$.ajax({
method: "POST",
url: "{{ route('Admin.Shop.Categories.moveTree') }}",
data: { node_id: node.id, type: position, target_id: target_node.id }
});
// console.log(e);
// console.log($('#tree1').tree('getTree'));
}
</script>
@endpush