draggable icon indicating copy to clipboard operation
draggable copied to clipboard

Saving positions via ajax

Open sanch941 opened this issue 7 years ago • 6 comments

Hi everyone. I cant find documentation of ajax . I need to save positions via ajax. How can i get array that contains all swappable items ?

sanch941 avatar Sep 10 '18 04:09 sanch941

I also want to know this.

brianabaker avatar Oct 16 '18 21:10 brianabaker

New position can be found at event.newIndex, so you should just make an Ajax request to save this within the sortable:stop callback. Just take into account that index is 0 based, in case you need it to start at 1.

sortable.on('sortable:stop', (event) => {
  console.log(event.newIndex);
  // Ajax request here
}

carlesjove avatar Jan 02 '19 22:01 carlesjove

Hi!

Is it possible to get the full array of the new index? For example, if I have ten list elements and I change one element, all positions for the other items could be changed depending on the new position. I cannot just update one item in the database, I need to update the full list.

bee-interactive avatar Jun 25 '19 21:06 bee-interactive

@bee-interactive There's not such functionality that I'm aware of, so you should probably handle this with event.newContainer by removing the 2 duplicated elements the action creates, which should have class draggable--original and draggable-mirror by default.

sortable.on('sortable:stop', (event) => {
  console.log(event.newContainer);
}

I guess it'd be nice to have a method that does exactly this…

carlesjove avatar Jun 26 '19 12:06 carlesjove

@carlesjove Yep that's too bad. This functionnality could be really helpful for this situation. Getting the event.newContainer could be a way around to achieve this but.. it's just a way around..

bee-interactive avatar Jun 26 '19 12:06 bee-interactive

function sortable() {

    var sortable = new Sortable('ul', {
        draggable: 'li',
    });

    sortable.on('sortable:stop', (event) => {
        var objs = [];
    
        var childrens = event.newContainer.querySelectorAll('a:not(.draggable--original):not(.draggable-mirror)');
    
        childrens.forEach((child, index) => {
            var uuid = child.getAttribute('data-uuid');
            var obj = {sort: index + 1, uuid: uuid};
            objs.push(obj);
        });
        
        // implementation save ajax...
    });

}

higorch avatar Oct 07 '23 17:10 higorch