Since my discovery of the awesome jQuery DataTables plug-in I haven’t stopped using it in my projects at it is by far the simplest and cleanest way of delivering feature-rich tables to a user.
Of course, I don’t usually work with static table data, meaning that I use server side processing scripts to load the data to my tables and when I started to employ user selectable data filters not connected to the DataTables object, I realised that I needed to be able to reload the DataTable’s data source at will.
However, simply trying to initialize a DataTable object on an existing DataTable object brings up the error message
DataTables warning: Unable to re-initialise DataTable. Please use the API to make any configuration changes required.
which is needless to say, a good indication that approach is not exactly going to get you anywhere.
The answer lies in exactly what the error message told you – you need to make use of the API calls on the existing DataTable in order to reload its data. So how to do this then?
Well essentially it is a pretty simple solution. First, we check if the known DataTables object exists, in other words, that it is not undefined. If it is undefined, initialize and draw it for the first time. If not, empty it first using the fnClearTable function and then redraw using the fnDraw function – which of course would then reload the updated dataset from the server side processing script.
if (typeof oTable == 'undefined') { oTable = $('#commentstable').dataTable({ "bProcessing": true, "bServerSide": true, "iDisplayLength": 150, "bLengthChange": false, "sAjaxSource": "datatables_comments_list.php" }); } else { oTable.fnClearTable( 0 ); oTable.fnDraw(); }
Nice!
Related Link: http://datatables.net/index
You might also enjoy:
-
If you are using the brilliant jQuery DataTables plugin to present your data in nifty dynamic tables, and are using it in a server-side loading context, you ...
-
I somehow missed this breezing through the available documentation on the DataTables website, but basically the question arose on how to force a table that ...
-
As it has quickly become apparent on this site, my two current favourites in the land of web development is the fantastic jQuery javascript library and the ...
-
I’ve mentioned the wonderful jQuery plugin DataTables a number of times before, the awesome little trick that instantly transforms any HTML table fed to it ...
-
Widget styling is a long, laborious and tedious job for the most part, which is exactly why it is so great that the guys behind jQuery UI came up with the T ...