Get a created datatable

Get a created datatable

Cesar BravoCesar Bravo Posts: 5Questions: 0Answers: 0
edited December 2009 in Bug reports
When a datatable object is created:

[code]
var oTable = $('#tableBug').dataTable(
{
"bAutoWidth": false,
"bLengthChange": true,
"bStateSave": true,
"bAutoWidth": false,
"aoColumns": [null, null, null, null],
"bLengthChange": true
}
);
[/code]

It only can be accessed due to the returned object, because if you call the datatable method again, it creates the controls twice.

[code]
var oTable2 = $('#tableBug').dataTable();
[/code]

One way to prevent this is to check in the constructor if the datatable object is created for this html element. The code will be:

[code]
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
return this.each(function()
{
for(var n=0; n<_aoSettings.length; n++) {
var sId = this.getAttribute( 'id' );
if(_aoSettings[n].sTableId == sId) {
return;
}
}

...
[/code]

Replies

  • allanallan Posts: 57,236Questions: 1Answers: 9,113 Site admin
    Hi Cesar Bravo,

    You are quite right, DataTables does now allow a table to be re-initialised by calling the constructor function again. A sanity check to prevent this happening (with an 'alert()' to let the developer know that something has gone wrong) is not a bad idea. I'll consider including this in a future version of DataTables. I'm also considering a 'destructor' API method which will 'nuke' an old table, so it would go well together.

    One thing to note with the code above, is that it assumes that there is an ID attribute on the table - which might not be true. Just something to watch out for if any one uses the above.

    Regards,
    Allan
This discussion has been closed.