Bizarre sorting problem with 1.9.4 and jQuery 1.9
Bizarre sorting problem with 1.9.4 and jQuery 1.9
danhunter
Posts: 6Questions: 0Answers: 0
I've noticed a weird bug when using custom sorting with dataTables 1.9.4 and jQuery 1.9.
When using the text from the DOM to define the sort order, using something like the following:
[code]
$.fn.dataTableExt.afnSortData['dom-text'] = function ( oSettings, iColumn, iVisColumn )
{
var aData = [];
$( 'td:eq('+iVisColumn+')', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
aData.push($(this).text());
} );
return aData;
};
[/code]
..things work fine with jQuery 1.8.2, but seemingly randomly with 1.9.1.
I setup a JSBin here: http://live.datatables.net/ivojev/edit#javascript,html and it works fine, as expected. However if I setup the same environment locally and use jQuery 1.9.1, the sorting on the appropriate content becomes erratic.
Not sure if this is a bug with jQuery 1.9's text method (though the output looks correct) or with the dataTables sorting.. or something in my setup.
When using the text from the DOM to define the sort order, using something like the following:
[code]
$.fn.dataTableExt.afnSortData['dom-text'] = function ( oSettings, iColumn, iVisColumn )
{
var aData = [];
$( 'td:eq('+iVisColumn+')', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
aData.push($(this).text());
} );
return aData;
};
[/code]
..things work fine with jQuery 1.8.2, but seemingly randomly with 1.9.1.
I setup a JSBin here: http://live.datatables.net/ivojev/edit#javascript,html and it works fine, as expected. However if I setup the same environment locally and use jQuery 1.9.1, the sorting on the appropriate content becomes erratic.
Not sure if this is a bug with jQuery 1.9's text method (though the output looks correct) or with the dataTables sorting.. or something in my setup.
This discussion has been closed.
Replies
> Before 1.9, sets that contained some connected and some disconnected nodes would be sorted inconsistently, depending on whether a disconnected node led the original unsorted set. As of 1.9, connected nodes are always placed at the beginning of the set in document order, and disconnected nodes are placed behind them.
That is also what DataTables wants, but doesn't appear to be what jQuery is doing - the disconnected nodes are actually inserted in the middle of the array, which is a bit odd. I think this is a bug in jQuery - I hope it is, as this is going to have a considerable knock on effect on me. I'll open a jQuery bug to see what they think about this.
Until then, here is a rewrite of the plug-in you are using to address this: http://live.datatables.net/ivojev/2/edit
[code]
$.fn.dataTableExt.afnSortData['dom-text'] = function ( oSettings, iColumn, iVisColumn )
{
var trs = oSettings.oApi._fnGetTrNodes(oSettings);
return $.map( trs, function (tr, i) {
return $('td:eq('+iVisColumn+')', tr).text();
} );
};
[/code]
Allan
Appreciate the new plugin code too, it works like a charm. Thanks also for your considerable and continuing work on DataTables.
[code]
return this.column( visColumn ).data();
[/code]
which will make life easier.
I presume you aren't just doing a text sort on the content though, but something a little more complex? Out of interest, what is that, if that is the case?
Allan
I found that sorting, using a custom sort method which updates the internal data to match the DOM, to be the best way to update DataTables after the models are updated (thus enabling me to update the filters based on the DataTables data, etc.) Rather than passing back all the updated data and calling fnUpdate for each. Is there a better way of doing this?
Not yet - there will be in 1.10 where you will be able to use specific properties / functions for each of the data types DataTables uses (filtering for example). You'd just specify where the sorting data is in the source object. You can sort of do this with mData as a function already, but there are other incompatibilities with Backbone and DataTables at the moment, which 1.10 should also resolve.
Allan
..this is true, though I've managed to get them working together quite happily, if a little redundantly. I used the following snippet that you suggested in another thread to provide the initial 'sync' with Backbone..
[code]
"fnCreatedRow": (nRow, aData, iDataIndex) ->
p = new Bluebase.Views.Product({el: nRow, model: view.collection.get(aData[0])})
[/code]
..and it's working pretty nicely so far. I look forward to seeing what 1.10 brings though (using a Backbone collection as the data source?)
Allan
No DataTables: http://live.datatables.net/uwidel/edit#source
DataTables: http://live.datatables.net/ivojev/4/edit
The new DataTables version shows that jQuery 1.9 is now always trying to order the nodes by document order, not but the order that they are found in the source array. Additionally removed nodes are appended to the results, themselves in reserve order, but that's just the way the Sizzle node ordering works when items are not in the document (try sorting numbers and you get the same result).
Given the suggestion in the release notes that the move is to have nodes always treated in document order, I don't think this is a bug, so not filing one. It is a bit frustrating that the nodes are iterated in a different order from the source array, but it is expected from their documentation and comments. So this is simply something we'll need to work around.
To that end, I've updated the examples and taken the unusual step of updating the examples on this site outside the normal release cycle. The examples in the 1.9.4 current release of DataTables can't change of course, but the updated examples will be in 1.10 when it is released.
Commit: https://github.com/DataTables/DataTables/commit/e00037a1af
Example: http://datatables.net/release-datatables/examples/plug-ins/dom_sort.html
Allan