Redraw table is appending data
Redraw table is appending data
Hello
I have a server side proccessed datatable and I just want to reload the data keeping the pagination and sort options. So I've read I have to use fnDraw function.
I've tried to do
[code]
$table.fnDraw(true);
[/code]
but with this I'm receiving the new data from the server and it's being appended to the last row of the table. I mean the old data hasn't been cleared.
I've tried to delete the old data with fnClearTable but with no succeed.
[code]
$table.fnClearTable();
$table.fnDraw(true);
[/code]
Can anyone tell me the way to do this?
Thanks.
I have a server side proccessed datatable and I just want to reload the data keeping the pagination and sort options. So I've read I have to use fnDraw function.
I've tried to do
[code]
$table.fnDraw(true);
[/code]
but with this I'm receiving the new data from the server and it's being appended to the last row of the table. I mean the old data hasn't been cleared.
I've tried to delete the old data with fnClearTable but with no succeed.
[code]
$table.fnClearTable();
$table.fnDraw(true);
[/code]
Can anyone tell me the way to do this?
Thanks.
This discussion has been closed.
Replies
Thanks.
$("#example").dataTable().fnDraw();
Assuming your $table object is a valid object (the same target as the initialization), it will work the same:
$table.dataTable().fnDraw();
If Allan or anyone else knows a better way, I'm all ears, too. No real performance issues with the way I'm currently doing it, but I'm always happy to make it even better. ;-)
Allan
I provide you with the code I'm using:
[code]
<%-- id --%>
Field1
Field2
Field3
Field4
Field5
Field6
<%-- hidden status --%>
<%-- place for button --%>
[/code]
and for the initialization of the table:
[code]
var $table = $("#mytable").dataTable({
"oLanguage": {
"sUrl": "/resources/js/datatables_lang/${pageContext.response.locale}.txt"
},
"bServerSide": true,
"sAjaxSource": "",
"bFilter": false,
"bSort": false,
"iDisplayLength": 100,
"iScrollLoadGap": 100,
"bScrollInfinite": true,
"sScrollY": ($(window).height() - 50) + "px", <%-- use with bScrollInfinite = true --%>
"aoColumnDefs": [
{"aTargets": [0], "sName": "id", "bVisible": false},
{"aTargets": [1], "sName": "field1"},
{"aTargets": [2], "sName": "field2"},
{"aTargets": [3], "sName": "field3"},
{"aTargets": [4], "sName": "field4"},
{"aTargets": [5], "sName": "field5"},
{"aTargets": [6], "sName": "field6"},
{"aTargets": [7], "sName": "status", "bVisible": false},
{"aTargets": [8], "sName": "id", "fnRender": linksRenderer, "bUseRendered": false, "sClass": "nowrap"}<%--this renderer just returns some links depending on the id value --%>
],
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.getJSON( sSource, aoData, function (json) {
<%-- Do whatever additional processing you want on the callback, then tell DataTables --%>
fnCallback(json)
} );
}
});
[/code]
And I've checked the json with Firebug and I'm seeeing that the number of the returned items as their data is OK. The problem is that the old data is not removed from the datatable.
In the document ready function, something like:
var $table = $('#example');
$table.dataTable( { my initialization } );
Then each time I want to update it, I simply call $table.dataTable().fnDraw();
In my case, I do it with a brute-force butt-simple Interval for polling:
var pollingInterval = setInterval('$table.dataTable().fnDraw()',2000);
You don't want to re-initialize the whole table. You just want to redraw it. I couldn't intelligently say why your method is breaking, all I can do is share my working example in the hopes that it helps. ;-)
[code]
$("#mytable").dataTable().fnDraw();
[/code]
May there be any problems because I have hidden columns?
var $table = $("#mytable").dataTable({ your full initialization });
is different than
var $table = $("#mytable");
$table.dataTable({ your full initialization });
During the initializing phase, the number of calculations is virtually identical. It's when you redraw that I'm not sure about, because the way you're doing it, you are calling fnDraw() on a full initialization object (your $table is an initialization). Which in turn may end up with the same results, mind you, though I suspect that it's computationally more expensive. Haven't tried it!
Greg