SortableJS / Sortable

Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
https://sortablejs.github.io/Sortable/
MIT License
29.59k stars 3.7k forks source link

Save only if something has changed #1746

Open whollaus opened 4 years ago

whollaus commented 4 years ago

Problem:

Is it possible to fire the event only if something has really changed? Currently my Ajax call is always called, even if you move the elements only a few millimeters. But without really changing anything.

Thanks!

store: {
  set: function (sortable) {
    let order = sortable.toArray();
    console.log(order);
    $.ajax({
      url: url,
      type: 'POST',
      data: {order: order},
      ...
    })
  }
}
waynevanson commented 4 years ago

Have you put a getter on it? That MAY be the issue. Otherwise, this is a bug according to the docs:

/**
 * Save the order of elements. Called onEnd (when the item is dropped).
 * @param {Sortable} sortable
 */
owen-m1 commented 4 years ago

@waynevanson This isn't a bug, he's asking if it could be called only if the list is changed onEnd. @whollaus For now you can just compare the sortable.toArray() before and after the save event is called, and only do your call if there is a change.

owen-m1 commented 4 years ago

I see no reason why the save function should be called if nothing has changed, so I'm marking this as an enhancement

Phoenixreturn commented 4 years ago

@owen-m1

I see no reason why the save function should be called if nothing has changed, so I'm marking this as an enhancement

Sorry, but I have the problem, that I want to insert element and see changes only drop not on dragover. Is it not the same?