How to retain the sort order after an fnDeleteRow?
How to retain the sort order after an fnDeleteRow?
One problem I'm having is that I am also implementing row deletion along with the nice drag-drop sort example found here:
http://www.jonathonjoyce.co.uk/2011/04/08/updating-datatables-display-orders-by-drag-and-drop/trackback/
... but the subsequent redraw following a fnDeleteRow causes the table row order to revert back to the original pre-sort row order (minus the deleted row). I am not reloading the table via ajax or anything. I am doing everything client-side for now.
Do I need to rebuild the underlying aoData array following the drag-drop sort? If so, how do I do that?
Any ideas about how I can retain the drag-drop-sorted row order persist AFTER a row delete? (without making a trip to the server).
http://www.jonathonjoyce.co.uk/2011/04/08/updating-datatables-display-orders-by-drag-and-drop/trackback/
... but the subsequent redraw following a fnDeleteRow causes the table row order to revert back to the original pre-sort row order (minus the deleted row). I am not reloading the table via ajax or anything. I am doing everything client-side for now.
Do I need to rebuild the underlying aoData array following the drag-drop sort? If so, how do I do that?
Any ideas about how I can retain the drag-drop-sorted row order persist AFTER a row delete? (without making a trip to the server).
This discussion has been closed.
Replies
Basically I think what is needed is for the sorting to update the DataTables internal cache and make use of aaSortingFixed which will force the sorted to be what is in your fixed column (and that would be the order of the rows).
Allan
So I gather that the first thing I should try is to turn on bSort? and then fiddle around with aaSortingFixed...
By the way, if it's any help, here is the simple code (lifted from the Jonathon Joyce example) for the jquery-ui drag-drop sort which sorts the UI, whether or not bSort is true/false:
[code]
$("#idOfTable tbody").sortable({
cursor: "move",
start:function(event, ui){
// a placeholder
},
update: function(event, ui) {
// a placeholder
}
});
[/code]
Here are the pertinent table/column init params:
"aaSortingFixed": [[0,'asc']],
"bSort": true
Obviously, I am setting column 0 as the column to used to contain the sort order. What is (kind of) interesting is that the column definition for column 0 can be "bSortable: true or false (doesn't matter). I guess aaSortingFixed overrides?
Anyway, here's the drag-n-drop update code that updates the underlying data and sort order:
[code]
$("#idOfTable tbody").sortable({
cursor: "move",
start:function(event, ui){
// a placeholder
},
update: function(event, ui) {
$("#idOfTable tbody tr").each(function(index) {
var pos = qTable.fnGetPosition(this); // get row position in current model
qTable.fnUpdate( index, pos, 0, false ); // false = defer redraw until all row updates are done
});
qTable.fnDraw();
}
});
[/code]
Regards,
Allan
[code]
update: function(event, ui) {
var i = 0;
var dataclone = $("#datatable-wrapper tbody tr").clone();
$(dataclone).each(function() {
var pos = oTable.fnGetPosition(this); // get row position in current model
var el = Array();
$(this).children().each( function() { el.push( $(this).html() );});
oTable.fnUpdate( el, i, 0, false ); // false = defer redraw until all row updates are done
i++;
});
oTable.fnDraw();
endPosition = ui.item.prevAll().length + 1;
}
[/code]