bRetrieve works incorrectly

bRetrieve works incorrectly

lvulvu Posts: 1Questions: 0Answers: 0
edited May 2012 in Bug reports
Hi!

There seems to be a bug in bRetrieve parameter handling, which leads to retrieval of an object different from the one created previously, although very similar. For example:
[code]
var tbl = $('table#data').dataTable();
tbl.foo = 'bar';
var tbl2 = $('table#data').dataTable({bRetrieve: true});
tbl2.fnDraw(); // this works
alert(tbl2.foo); // where is my foo?!
[/code]
This happens because when dataTable({bRetrieve: true}) is called, "return _aoSettings[i].oInstance;" statement inside .each() method does not do what expected. In fact, it does nothing, as .each() only pays attention to false-equivalent return values of its argument functio, and always returns "this" object.

Built-in dataTables functions work on retrieved object simply because it is almost completely initialized when it comes to bRetrieve checking. But it is not the same object.

The simple workaround, that also eliminates unnecessary object initialization, is to add to the very beginning of $.fn.dataTable function something like this:
[code]
// Looking for existing datatable if needed
if ( typeof oInit != 'undefined' && typeof oInit.bRetrieve != 'undefined' && oInit.bRetrieve === true )
{
var result = undefined;
this.each(function(){
for ( i=0, iLen=_aoSettings.length ; i

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The thing to remember here is that your tbl and tbl2 parameters are jQuery "arrays" of results - so just as $('td') !== $('td') the same applies to DataTables. So what is happening here is that you are attaching your 'foo' parameter to the array, not the DataTables instance.

    So what you can do is just use the first element in the array, which is persistent: http://live.datatables.net/omexal/edit#javascript,html

    Allan
This discussion has been closed.