Saving Nested Sortables with PHP
See original GitHub issueHi all,
I have very limited knowledge of JS (but decent knowledge of PHP). So I’m trying to properly pass to PHP the order of my nested items.
Problem:
What I have is a list of nested items, each with a hidden input field. So when I POST the form, I use the names and values of those fields in the order that they appear in the DOM.
<input type="hidden" class="menu-item-checkbox" name="menu-items[]" value="2"> <input type="hidden" class="menu-item-checkbox" name="menu-items[]" value="3"> <input type="hidden" class="menu-item-checkbox" name="menu-items[]" value="4">
In PHP I get these values into an array and then can do what I want with it. It all works great when there’s only one level of parent items. But the nested items make no difference.
This is the current JS code that I have to make nested sortables:
<script>
// Nested demo
var nestedSortables = [].slice.call(document.querySelectorAll('.nested-sortable'));
// Loop through each nested sortable element
for (var i = 0; i < nestedSortables.length; i++) {
new Sortable(nestedSortables[i], {
ghostClass: 'ghost-placeholder',
handle: '.handle',
group: 'nested',
dataIdAttr: 'data-id',
animation: 120,
fallbackOnBody: true,
invertSwap: true,
swapThreshold: 0.60,
emptyInsertThreshold: 30,
});
}
</script>
Due to my limited knowledge of JS, I have no idea how to pass the order of my nested sortables in any way as a POST field in my main form, or maybe as a separate AJAX request to a separate PHP file.
I have searched the documentation and existing/closed issues and I can’t seem to find any sample PHP code that would accomplish what I need.
I would prefer to save the order only once when the form is submitted, without having to keep a separate PHP file to handle separate AJAX requests.
Thank you all in advance for ANY guidance.
JSBin/JSFiddle demonstrating the problem:
What my HTML looks like: https://jsfiddle.net/s7dh2mko/
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Related StackOverflow Question
@dumitrub So why would that solution not work? It does exactly what you are asking. All you need to do is use this code:
Replace the identifier with the attribute on the HTML Element used to identify the item. For you it seems to be an
idof some sort. You will have to put the ID on the element asdata-id="id"in this case, and set theidenfitiervariable in that code to'id'. You say you want the children of the element to be an array accessible via thechildrenproperty of a serialized item, which is exactly what that code already does. ThenestedQueryshould be a CSS selector for the nested list (ie. the nested Sortable list container that is inside the item), Lastly, therootvariable should be set to the main Sortable container of the main list. Then you can callserializeand it will return the array.Perhaps PHP has an issue converting this serialized list to a PHP array? In that case, please share the corresponding PHP code.
If you use ‘parent_id’ logic for it there is an easy way. You can build parent_id’s like that, if you set an
data-parent-idto all sortable groupsYou can have an output like this.