From a6f86320ce298aa9ec46b8cd62ca6717bd7d7395 Mon Sep 17 00:00:00 2001 From: srosengren Date: Sat, 7 Mar 2015 21:35:52 +0100 Subject: [PATCH] Fixed issue with computed foreach's Fixed how we find correct indexes for items if the sortable is bound to a filtered collection. --- knockout-sortable.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/knockout-sortable.js b/knockout-sortable.js index 6a7f701..59d424d 100644 --- a/knockout-sortable.js +++ b/knockout-sortable.js @@ -67,7 +67,13 @@ //Unwrapping this allows us to manipulate the actual array 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. - 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 e.item.parentNode.removeChild(e.item); @@ -85,7 +91,7 @@ from.valueHasMutated(); } //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. to.valueHasMutated(); };