Datatables event on all tables on page

Datatables event on all tables on page

dreamer79dreamer79 Posts: 14Questions: 0Answers: 0
edited February 2012 in General
The plugin is great, but I just can't find the way to do a really simple thing. It seems that all the examples show a way to control tables when you assign a js variable for each of them. That's ok for most cases, but I need to have unknown number of tables on a page and make event handler for them all. If I want to use resize event examples say:
var oTable= $('.datatable').dataTable();
$(window).resize(function(){
oTable.fnAdjustColumnSizing();
});
OK. But what if I want to make something like:
$(window).resize(function(){
$('.datatable').fnAdjustColumnSizing();
});
I know this doesn't work and I just have no way to access datatables object for a table on page if I have no variable associated. The problem is that on a page I use ajax calls and create different tables with them dynamically in modal boxes. I even sometimes have 3 or 4 dialogs on top of each other and they all have tables. I use a simple script of mine for this moment, but data is large and everything runs slow, so datatables would be a great alternative as I may use server side processing. But is there a way to find a reference to the datatables objects from tables that have been converted? Using an array is not an option too as most tables will be destroyed and new ones will be created as you stay on page- I create an MVC style application.

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    There are a couple of ways of doing this at the moment, although making this much easier and more obvious in future is something that is very much on the cards for v1.10.

    How to do it atm:

    1. Loop over the $('.datatable') elements width jQuery's each. Then you can call $(this).dataTable().fnAdjustColumnSizing() on each.

    2. Use the $.fn.dataTablesSettings array which contains a copy of the settings object for every table on the page. You can loop over that and use the oInstance parameter to call the API method.

    Allan
  • dreamer79dreamer79 Posts: 14Questions: 0Answers: 0
    Yes, I just found out the second way(After reading plugin code comments.) and decided to post it so that others wondering how to iterate all tables could use it. My investigation didn't come to $.fn.dataTablesSettings, but
    [code]
    var s= $.fn.DataTable.settings;
    for ( i=0, iLen=s.length ; i
This discussion has been closed.