Browse Source

Fixed issue with computed foreach's

Fixed how we find correct indexes for items if the sortable is
bound to a filtered collection.
pull/497/head
srosengren 10 years ago committed by Sebastian Rosengren
parent
commit
593cc5a0f5
  1. 12
      knockout-sortable.js

12
knockout-sortable.js

@ -80,7 +80,13 @@
//Unwrapping this allows us to manipulate the actual array //Unwrapping this allows us to manipulate the actual array
var fromArray = from(), var fromArray = from(),
//It's not certain that the items actual index is the same as the index reported by sortable due to filtering etc. //It's not certain that the items actual index is the same as the index reported by sortable due to filtering etc.
originalIndex = fromArray.indexOf(itemVM); originalIndex = fromArray.indexOf(itemVM),
newIndex = e.newIndex;
if (e.item.previousElementSibling)
newIndex = fromArray.indexOf(ko.dataFor(e.item.previousElementSibling));
if (originalIndex > newIndex)
newIndex = newIndex + 1;
//Remove sortables "unbound" element //Remove sortables "unbound" element
e.item.parentNode.removeChild(e.item); e.item.parentNode.removeChild(e.item);
@ -98,7 +104,7 @@
from.valueHasMutated(); from.valueHasMutated();
} }
//Insert the item on its new position //Insert the item on its new position
to().splice(e.newIndex, 0, itemVM); to().splice(newIndex, 0, itemVM);
//Make sure to tell knockout that we've modified the actual array. //Make sure to tell knockout that we've modified the actual array.
to.valueHasMutated(); to.valueHasMutated();
}; };
@ -171,4 +177,4 @@
} }
}; };
}); });

Loading…
Cancel
Save