Check if a table element is datatables initialized

Check if a table element is datatables initialized

frenzyfrenzy Posts: 21Questions: 0Answers: 0
edited February 2011 in General
Hello there,

I want to check if a table element with say, id="datatable" is datatables-initialized. Something like this:
if ($(#datatable).dataTable().initialized) alert("initialized!") else alert("not initialized!");
How can I do that?
Thanks!

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    edited April 2011
    Heh - unfortunately that call will initialise any table which hasn't been already! There isn't a method for this yet, but a small loop should do the job:

    [code]
    function isDataTable ( nTable )
    {
    var settings = $.fn.dataTableSettings;
    for ( var i=0, iLen=settings.length ; i
  • frenzyfrenzy Posts: 21Questions: 0Answers: 0
    Thanks Allan! Exactly that is what i was looking for, i just thought there's a 'faster way' in a form of an included method, but this should do the trick. Thanks again!
  • hpachecohpacheco Posts: 19Questions: 0Answers: 0
    If there are multiple tables in the same page you should probably use
    [code]if ( settings[i].nTable == nTable )[/code]
    istead. Just came across that problem :-)

    TD;DR for lazy persons:

    [code]
    function isDataTable ( nTable )
    {
    var settings = $.fn.dataTableSettings;
    for ( var i=0, iLen=settings.length ; i
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Whoops! Thanks for catching that hpacheco! I've just modified my comment to match your code incase anyone else finds it and tries to use it as well!

    Regards,
    Allan
  • netaisllcnetaisllc Posts: 24Questions: 2Answers: 0
    +1
    --- What a truly helpful post and function. I'm using this as part of generalized jQuery based tab handler that is 'smart' enough to detect dataTables on the tab pane being displayed and invoke the columnAdjustment function. Spot on, Allan.
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Thanks for the feedback! I think I'll wrap this up into a static API method and include it in DataTables 1.9 actually - it is immensely useful to have available.

    Allan
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    i've just done some work on this for 1.9.1 - I've introduced two new static API methods:

    New: Static API method - fnIsDataTable - check if a TABLE node is a DataTable or not
    New: Static API method - fnTables - get the DataTables that are initialised on the table (optionally limit to just the visible tables)

    I think these will be really useful (I know in my own use of DataTables when working with fairly complex cases these would be useful) so are now in the core and will be included in 1.9.1.

    Regards,
    Allan
This discussion has been closed.