Row reorder update a subset of items
Row reorder update a subset of items

Hello I have a table where I have implemented row reorder, when an item is moved it shows the save button and upon clicking the save the new order is saved to the DB and the table is reloaded.
dataTable.on('row-reorder', function (e, diff, edit) {
var rowData = [];
for (var i = 0, ien = diff.length; i < ien; i++) {
rowData.push(dataTable.row(diff[i].node).data());
rowData[i].order = diff[i].newData;
}
if (!jQuery.isEmptyObject(rowData)) {
$('#btnSortOrder').show();
$('#btnSortOrder').click(function () {
saveData(rowData)
});
}
})
However, I have set each order to have incraments of 1000 so not every item in the table is updated. What I hope to get from this is if item 4, which would have order 4000, becomes item 1 then it would take the order 500 there only one item is edited and the order is still correct.
Before | After | |||
---|---|---|---|---|
1000 | item 1 | 500 | item 4 | |
2000 | item 2 | 1000 | item 1 | |
3000 | item 3 | 2000 | item 2 | |
4000 | item 4 | 3000 | item 3 |
I am a bit unsure how to figure out the logic of this, I would need to have an array of before and after to compare what order has changed and if it less than the current item or more. Or is this method going to get too complicated?
Replies
RowReorder doesn't work in that way. All the rows being moved have their indexes swapped with the row its replacing. You can see the behavior in this example:
https://datatables.net/extensions/rowreorder/examples/initialisation/simple.html
The first 4 rows start like this:
Move Tiger below Olivia and the indexes change like this:
You will need to update the indexes for all the rows involved in the reorder. The
diff
parameter in therow-reorder
event contains an array of the changed rows.Kevin