How do I define a secondary column to sort on per each column?
How do I define a secondary column to sort on per each column?
I'm having trouble to understand how to define a secondary column to sort on for each column. I have successfully added a secondary column for the initial sort when the table is loaded, but when the user resorts the table by cklicking the table column, it will only sort on that column and not any secondary column. How to define it?
I would like to define it per column level like, when column 0 is clicked, secondary would be column 5, column 1 clicked - sort secondary on column 2 etc etc... Is that even possible?
If not - at least tell me how to get a permanent secondary column to sort on...
Thank You!
I would like to define it per column level like, when column 0 is clicked, secondary would be column 5, column 1 clicked - sort secondary on column 2 etc etc... Is that even possible?
If not - at least tell me how to get a permanent secondary column to sort on...
Thank You!
This discussion has been closed.
Replies
but you can do this by catching the click and calling fnSort yourself with an array of sort directions
[code]
var oTable;
$(document).ready(function() {
oTable = $('#example').dataTable();
/* Sort immediately with columns 0 and 1 */
oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
} );
[/code]
http://www.datatables.net/ref#fnSort
I'll then need to do something like:
if (oTable.selectedColumnIndex == 1) { oTable.fnSort([ [1,'asc'],[5,'asc']]);}
it's the part with "selectedColumnIndex" I have to figure out now...
Here's a patch (to 1.8.1) that I believe allows the desired behaviour
[code]
--- DataTables/js/jquery.dataTables.js 2011-07-22 09:08:44.000000000 +1000
+++ DataTables/js/jquery.dataTables_as.js 2011-08-24 12:00:01.000000000 +1000
@@ -4640,15 +4640,24 @@
for ( i=0 ; i
Allan
The concept is that the one visible column is only sortable as a whole, ie. you can't sort by only one of it's backing columns, you're always sorting on both backing columns. That should alleviate any confusion from the end user because the behaviour will be consistent (as per the developers definition).
As you noted this patch doesn't handle opposite direction sorting for the multiple iDataSort columns (my use case was based on start/end dates, so same direction is all I needed), though it could be handled by making iDataSort an array of arrays similar to aaSorting. Arguments for this include making the interface consistent with aaSorting and the obvious added flexilibility. Arguments against are the small number of uses and the amount of work required (would probably need to poke around in fnInnerSorting to get that going, I just don't have that itch right now).
I've created a discussion in the feature request category so perhaps we could continue there? http://www.datatables.net/forums/discussion/6326/multi-valued-idatasort
[code]
$(document).ready(function() {
$('#example').dataTable( {
"aoColumnDefs": [ {
"aDataSort": [ 0, 1 ],
"aTargets": [ 0 ]
} ]
});
} );
[/code]
In this code the first column will sort on column index 0 first and then column index 1 secondly (if needed).
In terms of sorting direction both columns will be sorted in the same direction (i.e. either ascending or descending). If you want to define a different sorting direction you would need to do an override such as that described above to use fnSort manually, but I think that the implemented method will cover the majority of cases.
All thoughts on this are most welcome, as always!
This is now available in the 1.8.3.dev build (although that might become 1.9) available on the downloads page if anyone wants to try it. Note thought that it is currently incompatible with ColReorder. All other plug-ins should work okay.
Regards,
Allan
Thanks!
Allan