Dynamic Default Sort Columns
Dynamic Default Sort Columns
bryceray1121
Posts: 65Questions: 0Answers: 0
My goal is to be able to define a default sort column. I have multiple tables and each table will have a different column as the default sort. This rules out using aaSorting to define the default sort because it requires an index which will be used for every table. I'm assuming something like this is not possible:
[code]
"aaSorting": [
["aTargets":["dataTable-defaultSort"],"desc"]
],
[/code]
I've also tried using aoColumnDefs, however I may be misunderstanding the purpose of asSorting. In this example I've assigned a predetermined class to each column in each table I want to default sort to.
[code]
"aoColumnDefs": [
{"asSorting": ["desc"],"aTargets":["dataTables_defaultSort"]}
]
[/code]
Neither of these approaches seem to work. Is there another way I can approach this problem? Or do I need to develop my own functionality for this feature?
Thanks.
[code]
"aaSorting": [
["aTargets":["dataTable-defaultSort"],"desc"]
],
[/code]
I've also tried using aoColumnDefs, however I may be misunderstanding the purpose of asSorting. In this example I've assigned a predetermined class to each column in each table I want to default sort to.
[code]
"aoColumnDefs": [
{"asSorting": ["desc"],"aTargets":["dataTables_defaultSort"]}
]
[/code]
Neither of these approaches seem to work. Is there another way I can approach this problem? Or do I need to develop my own functionality for this feature?
Thanks.
This discussion has been closed.
Replies
Allan
This is what I have so far:
[code]
function setDefaultSort(){
var dataTables = getElementsByClassName("dataTable");
for(var i=0;i<(dataTables.length);i++){
var columns = dataTables[i].getElementsByTagName("th");
for(var x=0;x<(columns.length);x++){
if(jQuery(columns[x]).hasClass("dataTable-defaultSort")){
$.fn.dataTableExt.iApiIndex = i;
oTable.fnSort([x,'desc']);
}
}
}
$.fn.dataTableExt.iApiIndex = 0;
}
[/code]
This correctly identifies columns with the class that I use to distinguish a column as a default sort column. However, when it tries to apply the sort to the column I am seeing an error:
"oSettings is null"
Any suggestions on what might be causing this?
Where is oTable coming from? Is it a global variable? What might be easier, rather than using the API Index is something like:
[code]
function setDefaultSort(){
var dataTables = getElementsByClassName("dataTable");
for(var i=0;i<(dataTables.length);i++){
var columns = dataTables[i].getElementsByTagName("th");
for(var x=0;x<(columns.length);x++){
if(jQuery(columns[x]).hasClass("dataTable-defaultSort")){
$(dataTables[i]).dataTable().fnSort([x,'desc']);
}
}
}
}
[/code]
Allan
Allan
[code]
var oTable = renderDataTable(".dataTable"); //Render data tables
setDefaultSort(); //Sort all columns based on class value
function renderDataTable(id) {
var oTable2 = jQuery(id).dataTable({
"sScrollY": "200px",
"sScrollX": "100%", /* Required for viewing tables with lots of columns at low resolution - otherwise columns are mis-aligned */
"bScrollCollapse":true,
"sDom": 'rt<"bottom"flp>',
"bPaginate": false,
"bStateSave": false,
/*"bSortClasses": false,*/
"aoColumnDefs": [
{"bSearchable": false, "bSortable": false, "aTargets": ["dataTable-exclude"]},
{"bVisible":false, "aTargets" : ["dataTables_notVisible"]}
]
} );
return oTable2;
}
function setDefaultSort(){
var dataTables = getElementsByClassName("dataTable");
for(var i=0;i<(dataTables.length);i++){
var columns = dataTables[i].getElementsByTagName("th");
for(var x=0;x<(columns.length);x++){
if(jQuery(columns[x]).hasClass("dataTable-defaultSort")){
$(dataTables[i]).dataTable().fnSort([x,'desc']);
}
}
}
}
[/code]
However, I think that this is slightly inefficient - since it will cause every table to be drawn twice. How about:
[code]
var oTable = renderDataTable(".dataTable"); //Render data tables
function renderDataTable(selector) {
var out = [];
var tables = jQuery(selector);
var sorting;
for ( var i=0, iLen=tables.length ; i
The error firefox spits out is:
oColumn is undefined
Several columns are set to be hidden in the dataTable init. Is it possible the index of the default sort columns is changing because they are hidden before they are sorted?
[code]
jQuery('th', tables[i]).index("th.dataTable-defaultSort")
[/code]
Having said that - I don't see where it is... Although - perhaps this might do it:
[code]
jQuery('th', tables[i]).index("th.dataTable-defaultSort", tables[i])
[/code]
If not - then it would be worth echoing out the result of that expression and seeing what it is returning.
Also, the way the code is above, it shouldn't have any impact
Regards,
Allan
[code]
function renderDataTable(selector) {
var out = [];
var tables = jQuery(selector);
var sorting;
for ( var i=0, iLen=tables.length ; i= 0){
sorting = [ defaultCol, 'desc' ];
}else{
sorting = [0,'desc'];
}
var oTable2 = jQuery(tables[i]).dataTable({
"aaSorting": [ sorting ],
"sScrollY": "200px",
"sScrollX": "100%",
"bScrollCollapse":true,
"sDom": 'rt<"bottom"flp>',
"bPaginate": false,
"bStateSave": false,
"aoColumnDefs": [
{"bSearchable": false, "bSortable": false, "aTargets": ["dataTable-exclude"]},
{"bVisible":false, "aTargets" : ["dataTables_notVisible"]}
]
} );
out.push( oTable2 );
}
return out;
}
[/code]
Quite frankly I'm surprised that this functionality (being able to specify initial sorting based on class name) isn't a core feature. It seems to me that DataTables configuration relies a lot on being able to specify columns by index which in reality means that every DataTables instance have to be tailored to each specific case where it's used.
because all my tables are being sorted by date which i want to put in last column and in descending order as well?
> It seems to me that DataTables configuration relies a lot on being able to specify columns by index which in reality means that every DataTables instance have to be tailored to each specific case where it's used.
I agree - DataTables 1.10 is going to make naming columns and using those names much easier: http://datatables.net/development/roadmap :-).
@evoxix
> is it possible to do this without using an index?
Currently no - you need to work out the index I'm afraid. A line or two of Javascript should be able to do that. DataTables 1.10 should make this much easier.
Allan