Creating new table through ajax and setting it to datatable

Creating new table through ajax and setting it to datatable

SurtidoSurtido Posts: 5Questions: 0Answers: 0
edited August 2010 in General
Hi,

first, your code is great! datatables is the best jquery plugin i never used.
I'm populating a page with tables, through ajax. I 'm looking to make that new tables "datatables", but my js says is imposible to re-initialize the datatables.
All my datatables have the same class ".datatable" so my call on load is:
[code]
jQuery('.datatable').dataTable();
[/code]

every time i add a table through jquery .load() method, i add to the callback that call: jQuery('.datatable').dataTable(); but here is when the error comes.

How can i set new tables as datatables after callbacks with .load()?

Replies

  • SurtidoSurtido Posts: 5Questions: 0Answers: 0
    bump

    any help would be appreciated.
  • XstreamINsanityXstreamINsanity Posts: 23Questions: 0Answers: 0
    Do these tables have unique names that you can access? If you're using a static string and adding multiple tables and each time calling the initialize function, it will not work. It is ideal to have every table have a unique name and initialized that way. Having multiple tables with the same name and only initializing once has not worked well for me. :)
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Multiple tables based on a single selector most certainly should work: http://datatables.net/examples/basic_init/multiple_tables.html

    However, the interaction can become complicated if you are using that selector on every initialisation. As the error says, you cannot re-initialise a DataTable (you can destroy it and then create it again if you want to do that). So for example:

    [code]
    jQuery('.datatable').dataTable();

    // ... some code to create a table (and not remove the old ones) ...

    jQuery('.datatable').dataTable();
    [/code]
    will not work. Perhaps you can provide a link to show us how you are doing the initialisation?

    Allan
  • compsultcompsult Posts: 13Questions: 0Answers: 0
    Hi Allan,

    First, thank you for such a great product. I am doing the same thing as Surtido (using load() to populate a div with a new table). I reviewed how multiple tables are initialized in the example you link to above. My query is this. If one of those tables were loaded dynamically (i.e., after the page had loaded, using ajax), would it be initialized?

    Best
    Mike
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Hi Mike,

    It might be... The table will only be initialised if it is available in the DOM (i.e. if the selector $('.datatable') matches your table). If it doesn't then there is no way for DataTables to know that you are going to add a table to the DOM in future (there is no "live" support) so no action can be taken on it. You must have the table in the DOM to be able to initialise it.

    Allan
This discussion has been closed.