Deactivating DataTables dynamically

Deactivating DataTables dynamically

tim.vishertim.visher Posts: 1Questions: 0Answers: 0
edited December 2009 in General
Is it possible to deactivate DataTables once it's been called? I'm trying to create a printable page out of a page with many DataTable formatted tables and I'd like to just turn DataTables off if I can.

Thanks in advance!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi tim.visher,

    There currently isn't a 'destructor' method for DataTables, although it's something that I hope to add into a future release.

    I know that doesn't help you much (!), but hopefully this will. There is a plug-in for DataTables called TableTools which provides a 'print' option for the table (as well as a couple of other options, although they can be disabled if you wish): http://datatables.net/release-datatables/extras/TableTools/ . This integrates well with the table, and provides interaction for the end user to get a printable table, without loosing all their sorting, filtering etc.

    Regards,
    Allan
  • ewelleinewellein Posts: 3Questions: 0Answers: 0
    edited January 2010
    my question may or may not fall under this category - basically, i want to use datatables with a dynamically created table.
    see my code on the source of http://www.tscnyc.org/trash_datatables.php

    if you click and run the function that creates the tables, i did a settimeout that runs the init function for datatables, it causes no errors after the 5 seconds, but it fails to see the newly created rows...

    any ideas on how to make it see / include rows that have been created using javascript / not already printed to the page? (or is it that datatables can only see dynamically created rows using jquery?)

    thanks!

    Ed Wellein
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Ed,

    I'm slightly confused - sorry... To create a DataTables dynamically, it's basically the same as creating it at page load time. Have the table in the DOM, and then initialise DataTables on top of it. Or you could do something like this to have it 100% Javascript: http://datatables.net/examples/data_sources/js_array.html .

    Also you can add new rows to a DataTables which has already been created like this: http://datatables.net/examples/api/add_row.html

    Regards,
    Allan
  • ewelleinewellein Posts: 3Questions: 0Answers: 0
    thanks for your quick response Allan,

    i'll have to read up on what it means to 'have the table in the DOM' - if the tables have been displayed on the screen, you'd think they'd be in there. and then once the function is done to display them, i run the function that contains the DataTables initialisation.

    i will perhaps have to go the array route as you mention, or rewrite the code to create the rows, etc. using your functions, so they're compatible with DataTables.

    just so you understand my issue - here are some isolated codes that demonstrate -

    1. this works - http://tscnyc.org/jquery_works.php - if you click the initialize link and then click on the column headers it sorts asc/desc (returns 1 to 3 of 3 entries) - this table is just coded there on the page in html.

    2. but this example doesn't work - http://tscnyc.org/jquery_works_not.php - the datatables plugin fails to 'see' those newly created rows (returns 0 to 0 of 0 entries)... - this table is dynamically created just prior to running the initialisation code.

    thanks again for your help.

    Ed.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Ed,

    What I meant by "have the table in the DOM" basic basically have the table in HTML, before you initialise DataTables. If your html file (generated by php, C#, static whatever) has the table in it, then DataTables should initialise fine.

    One thing to note as well is that DataTables requires the TBODY element - odd things can happen if that is not included: http://datatables.net/usage/ .

    When your page loads, do you have an HTML table in it, and you what to enhance it with DataTables? With the ability to add new rows? That is basically covered by the 'add rows' example link above. Or are you look for a different use case?

    Regards,
    Allan
  • ewelleinewellein Posts: 3Questions: 0Answers: 0
    hi Alan,

    if you click that link for my #2 example, you'll see the dynamically created tables rows will show on the screen, but if you view source you won't see the actual tags, etc. you'll just see the loops in javascript that create the rendering on the screen. so the rows are 'kinda' in html, but not really in the manner required by datatables i guess.

    i can understand what needs to be done - redoing my code so that the rows are more 'real' - like the javascript array idea / add rows examples you put forth, etc.,

    i will add in the tbody element as well.

    thanks again for your assistance!

    Ed.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Ed,

    Using a table created by Javascript most certainly should work with DataTables - indeed here is an example which added a TD element to each row before the table initialised: http://datatables.net/examples/api/row_details.html . It's just a case of making sure that it is correctly constructed.

    If it's just a case of wanting to add rows dynamically, then you could just use the fnAddData function after initialising an empty table. But either way should work fine :-)

    Regards,
    Allan
This discussion has been closed.