Splice() Not Updating Items Order Of Array Within Knockout.js
Solution 1:
Almost there. Here's how I did it.
When moving an item up, you need to swap it with the previous item. Thus, you need to replace the elements at indices i-1
and i
with array[i]
and array[i-1]
, respectively. Your moveup
method does exactly that, so all is good.
Now, when moving an item down, you need to swap it with the next item. Thus, you replace the elements at indices i
and i+1
with array[i+1]
and array[i]
, respectively. Your code however changes the elements i+1
and i+2
, which is no good. Instead, you should be doing:
self.itemList.splice(i, 2, array[i+1], array[i]);
You start splicing at i
(as you're removing the elements at i
and i+1
) and you replace them (insert at that index) with array[i+1]
and array[i]
.
On another note, your check for whether you can move the item down is incorrect. The only item you should not move down is the last item, i.e. the element at index self.itemList().length-1
. Thus, the check should look like if (i < array.length - 1) { ... }
(see the fiddle).
Post a Comment for "Splice() Not Updating Items Order Of Array Within Knockout.js"