Store reference to dataTable object

Store reference to dataTable object

philipstreetphilipstreet Posts: 5Questions: 0Answers: 0
edited January 2011 in General
Hi All,

Config : using jQuery v1.3.2 and DataTables 1.5.4 - I can't upgrade to the latest versions as that breaks all my tables.

I want to be able to store a reference to the dataTable object so that I can retrieve it later. For example, I want to do something like this;

[code]
$(document).ready(function() {
var $container = $("#container");
var $dt = $("#table").dataTable();
$container.data("dt", $dt);
}

function doSomethingGenericWithADataTable(container) {
var $currentDT = $(container).data("dt");
// blah blah blah
}
[/code]

The reason being is that I'm creating a ASP.NET User Control containing a DataTable, and this control will be used several times on the same ASPX page. I'm trying to write as much generic code as possible in the control for performing the various operations on the DataTable. So I need to store references to the DataTables somehow so I can retrieve and use them later.

Kind regards,

Phil

Replies

  • philipstreetphilipstreet Posts: 5Questions: 0Answers: 0
    I've sorted my problem by not initially storing the dataTable object in a jQuery variable, i.e. using a normal JS variable. See below...

    [code]
    $(document).ready(function() {
    var $container = $("#container");
    var dt = $("#table").dataTable();
    $container.data("dt", dt);
    }

    function doSomethingGenericWithADataTable(container) {
    var $currentDT = $(container).data("dt");
    // blah blah blah
    }
    [/code]

    Quite simple really, I suppose.

    Phil
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Or you can call $("#table").dataTable() again and get the original reference to the table object.

    Allan
  • philipstreetphilipstreet Posts: 5Questions: 0Answers: 0
    Hi Allan,

    Thanks for your reply.

    I tried that already but each time I do that the DataTable header and footer are added to the table. That might be a quirk of the version (1.5.4) I'm using.

    Regards,

    Phil
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Ah yes - this ability was adding in DataTables 1.7. 1.6 will give an error, while 1.5 will just go bonkers :-)

    Allan
  • philipstreetphilipstreet Posts: 5Questions: 0Answers: 0
    Hi Allan

    I believe "bonkers" is an accurate description of what I've been experiencing... :-)

    Phil
This discussion has been closed.