Sortable: Calculate indexes of all the items in the list after drag

Created on 18 Mar 2016  路  3Comments  路  Source: SortableJS/Sortable

This is not an issue, but a question , sorry for posting it here and thanks in advance.

  • I'm implementing a simple list each has priority values sorted by ascending order. When I drag one list to another, I want to recalculate updated priorities. How do I get all indexes of the list, I only see evt.oldIndex and evt.newIndex for the item which is being dragged.

Thanks!.

Most helpful comment

Thanks matt,

managed to do it via onUpdate. I couldnt use toArray, as i always needed first one to remain with data-id=0 in order to save his possition as first one.

So my code was:

onUpdate: function (evt) {
    [].forEach.call(evt.from.getElementsByClassName('omaido__sortable_item'), function (el,index) {
        el.setAttribute("data-id",index);
    });
},

this way, data-id was always from 0 to how many i have, while items changed around.

All 3 comments

Use toArray

Hi @rakeshrockb,
Here's what I've done, and it works pretty well. The second parameter of the onEnd event is the index of each item.

$scope.SortableOptions = {
  onEnd: function (evt) {
    angular.forEach(evt.models, function(obj,index){
      //do something with the index
    });
  },
};

Thanks matt,

managed to do it via onUpdate. I couldnt use toArray, as i always needed first one to remain with data-id=0 in order to save his possition as first one.

So my code was:

onUpdate: function (evt) {
    [].forEach.call(evt.from.getElementsByClassName('omaido__sortable_item'), function (el,index) {
        el.setAttribute("data-id",index);
    });
},

this way, data-id was always from 0 to how many i have, while items changed around.

Was this page helpful?
0 / 5 - 0 ratings