More on DataTables issue: bStateSave=true causes "Cannot read property 'asSorting' of undefined"

More on DataTables issue: bStateSave=true causes "Cannot read property 'asSorting' of undefined"

devgreerdevgreer Posts: 9Questions: 0Answers: 0
edited December 2013 in DataTables 1.9
I know this problem with bStateSave causing dataTables to crash has been beat to death, but as far as I can find, nobody's ever posted any reason for it, just some handwaving about conflicts. I decided to have a look under the hood, and instead of using jquery.dataTables.min.js, I used jquery.dataTables.js. In that case, the error happens here, at line 6701 of 1.9.4:

[code]
/* If aaSorting is not defined, then we use the first indicator in asSorting */
if ( oInit.aaSorting === undefined && oSettings.saved_aaSorting === undefined )
{
oSettings.aaSorting[i][1] = oColumn.asSorting[0];
}
[/code]

Where oColumn is undefined. It's supposed to have been defined at 6690:

[code]
var oColumn = oSettings.aoColumns[ oSettings.aaSorting[i][0] ];
[/code]

Putting a log line in there, console.log(oSettings), we find that oSettings.aaSorting[i][0], which is supposed to be the index into oSettings.aoColumns, is having a problem because according to the log dump of oSettings:

[code]
aaSorting: "[[1, "desc", 0]]"
[/code]

That's not an array, that's a string! So there's no way to get an index of [i][0] into that, so it's undefined, so oColumn is undefined, and the whole thing crashes.

But I don't know why aaSorting comes out that way. In our settings var, aaSorting is defined like this:

[code]
aaSorting: [[ 1, "desc" ]],
[/code]

So it looks like there's a JSON problem, maybe? It looks like it's not getting encoded or decoded properly. Apparently when people simplify their code, the problem goes away, but that's not really an option for us, nor is linking to a page that causes the problem. Anybody have an idea why JSON would get derailed like this? Or how to change the dataTables code to deal with this?

Replies

  • techpeacetechpeace Posts: 5Questions: 0Answers: 0
    I can report seeing this issue as well. I ran the same analysis and found the same issue:

    [code]oSettings.aaSorting[/code] is being stored as the string [code]"[[11, "desc", 1]]"[/code]
  • techpeacetechpeace Posts: 5Questions: 0Answers: 0
    Upon further investigation, it looks like this is a conflict caused by Prototype redefining the toJSON method of Array: http://stackoverflow.com/a/6611126/109663
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Ouch! Never a good idea to modify prototypes like that... If you remove Prototype does it work?

    Allan
This discussion has been closed.