How to get dataTable object from code?

How to get dataTable object from code?

jhuber151jhuber151 Posts: 5Questions: 0Answers: 0
edited August 2012 in General
Hey everyone, I hope I have a pretty easy question here. I am building a site where i use dataTable. When the page first loads i create a table like

[code]
var oTable = $('.class_path', pane).dataTable({
"sDom": 'Rt',
"bJQueryUI": true,
"sScrollY": "300px",
"bPaginate": false,
});
[/code]

Then later i rip out part of the page that contains the table using .detach and place it somewhere else. This is where the problem lies. When the page gets set the headers of the dataTables are out of line. From there i thought i would be able to call fnAdjustColumnSizing() and fnDraw() to fix the issue. The issue when this is formed it creates one table for the header and one for the scroll body. I am not sure which one to use as the selector (if either).

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    The fnTables static method will get you a reference to all tables which are DataTables:

    [code]
    var table = $.fn.dataTable.fnTables(true);
    if ( table.length > 0 ) {
    $(table).dataTable().fnAdjustColumnSizing();
    }
    [/code]

    (note that only works with 1 table, you might need to loop over the table array if you have more than one?).

    Allan
  • jhuber151jhuber151 Posts: 5Questions: 0Answers: 0
    Thanks for the quick reply. I tried placing the code in, but i am getting an error TypeError: $.fn.dataTable is undefined. I am using dataTables 1.9.2. The only thing i forget to mention in the last post is that I decided to place the table in a child window. I am importing dataTables 1.9.2 into the child page. I am not sure if it complicates things. Is there a specific way to call the fnTables that may be slightly different?
  • jhuber151jhuber151 Posts: 5Questions: 0Answers: 0
    I figured out that i wasn't importing the correct file for dataTables. So now i am able to call the fnTables in the child window, now the issue is that the array of tables are coming up empty. Any ideas?
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    If you are passing in true, like my example above, then you are only getting visible tables and an empty array might suggest that there are no visible DataTables when you call it?

    Allan
  • jhuber151jhuber151 Posts: 5Questions: 0Answers: 0
    It would seem that they are empty. I know it has something to do with the .detach function. I run
    [code]
    var table = $.fn.dataTable.fnTables(true);
    console.log(table);
    [/code]

    on both the child and parent window about 5 seconds after loading the child window and it gives back that there are 3 tables on the parent window (which i started off with 2 tables. So there should only be 1 now) and 0 tables on the child window. This is just a confusing issue and I might have to try a different way.

    Thanks Allan!
This discussion has been closed.