Manualy initialising datatables

Manualy initialising datatables

stetheyidstetheyid Posts: 8Questions: 0Answers: 0
edited November 2010 in General
I currently have the following:

[code]
$(document).ready(function() {

// Initialise Table
objTable = $('#Data').dataTable( {
......................
[/code]

so that the table is built onload of the page.

what is the easiest way to "initialise" the table but not build the content until a form element is clicked on my page?

thanks.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I'm not quite sure what you mean - sorry. You can initialise the table at any time by using $().dataTable() - be it in a callback or event handler or whatever. You can also add and delete data from the table at any time using the API methods.

    Allan
  • stetheyidstetheyid Posts: 8Questions: 0Answers: 0
    Probably my JS knowledge holding back my explanation!

    I have the datatables code within
    $(document).ready(function() {

    This initialises the table - in some cases I want the table to be drawn but no ajax request fired off until the user clicks a button
  • stetheyidstetheyid Posts: 8Questions: 0Answers: 0
    Is the better approach to put the datatables code into a standard JS function which is called onload if required and via an event handler if it is to be loaded on demand?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I would tend to say yes - your latter suggestion sounds like a good way of doing it (unless the table is visible before that, in which case you probably want it initialised when it's visible and then use the API methods). There is nothing to stop a DataTable being initialised at any time :-)

    Having said that, it is possible to block the first XHR made by DataTables by using the fnServerData initialisation parameter and just putting in the logic that you want.

    Allan
  • stetheyidstetheyid Posts: 8Questions: 0Answers: 0
    Thanks Allan.

    Can you expand on your last suggestion please? If I dont initialise the table onload I dont get any style etc (it looks nasty) so I'd like to (in the cases where I dont want the data loading immediately) load the table but have no data until a button is clicked.

    Blocking the first XHR made sounds perfect
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    http://datatables.net/usage/callbacks#fnServerData - as shown you can construct your own Ajax call to the server. So all you need to do is add an if cndition in there to say that if it's the first draw just give the callback method an empty data set. Otherwise, do the Ajax call to the server.

    Allan
This discussion has been closed.