如何使用 SortableJS 库提交新排序的元素列表

How to submit newly sorted list of elements, with the library SortableJS

我正在使用 JS 库 SortableJS 并尝试在提交时发送新排序的数组。

JS:

var originalArray;
var sortable = Sortable.create(selection, {
  handle: '.bars-move',
  animation: 150,
});

var modalForm = document.getElementById("modalForm");
  if (modalForm  !== null ){
    modalForm.addEventListener('submit', function(e) {
      var formData = new FormData(modalForm);
      e.preventDefault();
      var request = new XMLHttpRequest();
      request.open(modalForm.method, modalForm.action, true);
      var cookies = parse_cookies();
      request.setRequestHeader('X-CSRFToken', cookies['csrftoken']);
      request.onload = function() {
        /// send the new Array here
      };
      request.send(formData);
    });
  };

欢迎任何提示!

最简单的方法就是在提交时提交可排序数组:

var sortable = Sortable.create(selection, {
    handle: '.bars-move',
    animation: 150,
  });

var modalForm = document.getElementById("modalForm");
if (modalForm  !== null ){
    modalForm.addEventListener('submit', function(e) {
      var formData = new FormData(modalForm);
      e.preventDefault();
      var request = new XMLHttpRequest();
      request.open(modalForm.method, modalForm.action, true);
      var cookies = parse_cookies();
      request.setRequestHeader('X-CSRFToken', cookies['csrftoken']);
      formData.append('sortableArray', sortable.toArray());
      request.send(formData);
    });
};