How do I change the row order of table using knockout. I have the fiddle:
Using knockout-sortable.js. Example
Please assist. My fiddle is below:
JSFiddle Example
[1]:http://jsfiddle.net/rniemeyer/hw9B2/
[2]:https://jsfiddle.net/nagarajputhiyavan/x9vc10zu/3/
Since the whole idea of Knockout is that you work with your data model and Knockout keeps the UI in sync with it, the fundamental problem is simply reordering your array (and it needs to be an
observableArrayfor Knockout to notice changes to it).Of the two methods you suggested, the Up and Down buttons are easier, so that's what I went with. I added Up and Down buttons to each row, and disabled Up for the first row and Down for the last row.
The Up button is
click-bound to amoveUpfunction that splices out the current row and splices it back in a row up. The Down does the same thing, but splicing back in a row down.Using Drag and Drop would be somewhat more complicated, because the re-ordering happens in the UI and needs to be reflected into the data model. You would do it in a custom binding handler, and such a handler has been written. Here is an article about it.