Cannot custom sort with Ajax Data
Cannot custom sort with Ajax Data
Seems like my sorting function isn't even called.
Not sure what sorting is happening but the fields change order if I
change the enum order in db.
Thanks.
function fnSort(a){
if (a == "ERROR"){ return 1; }
else if ( a == "NEW"){ return 2; }
else if ( a == "PLACED"){ return 3; }
else if ( a == "SHIPPED"){ return 4; }
return 15;
}
jQuery.fn.dataTableExt.oSort['orderstatus-asc'] = function(a,b) {
var x = fnSort(a);
var y = fnSort(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['orderstatus-desc'] = function(a,b) {
var x = fnSort(a);
var y = fnSort(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
oTable = $('#orders_table').dataTable( {
"bJQueryUI": true,
"bProcessing": true,
"bServerSide": true,
"aaSorting": [[3,'asc'],[4,'asc']],
"iDisplayLength": 100,
"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"sPaginationType": "full_numbers",
"sDom": 'T<"clear"><"fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix"lfr>t<"fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix"ip>',
"oTableTools": {
"sSwfPath": "media/copy_cvs_xls_pdf.swf",
"aButtons": [
"copy",
"print",
{
"sExtends": "collection",
"sButtonText": "Save",
"aButtons": [ "csv", "xls", "pdf" ]
}
]
},
"aoColumnDefs": [
{"bSortable": false, "aTargets": [0]},
{"bVisible": false, "aTargets": [1]},
{"sClass": "center", "aTargets": ["_all"]},
{
"fnRender": function (oObj) {
var sValue = oObj.aData[2];
var aDate = sValue.split(' ');
var fDate = aDate[0].split('-');
return fDate[1]+"/"+fDate[2]+"/"+fDate[0];
},
"bUseRendered": false,
"aTargets": [2]
},
{"sSortDataType": "orderstatus", "aTargets": [3]},
],
"sAjaxSource": "data/getData.php",
"fnInitComplete": function (){
}
});
// Row Details
$('#orders_table').on('click', 'tbody td img', function(){
var nTr = this.parentNode.parentNode;
if (this.src.match('details_close')){
/* This row is already open - close it */
this.src = "images/details_open.png";
oTable.fnClose(nTr);
} else {
/* Open this row */
this.src = "images/details_close.png";
oTable.fnOpen(nTr,fnFormatDetails(nTr),'details');
}
});
Not sure what sorting is happening but the fields change order if I
change the enum order in db.
Thanks.
function fnSort(a){
if (a == "ERROR"){ return 1; }
else if ( a == "NEW"){ return 2; }
else if ( a == "PLACED"){ return 3; }
else if ( a == "SHIPPED"){ return 4; }
return 15;
}
jQuery.fn.dataTableExt.oSort['orderstatus-asc'] = function(a,b) {
var x = fnSort(a);
var y = fnSort(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['orderstatus-desc'] = function(a,b) {
var x = fnSort(a);
var y = fnSort(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
oTable = $('#orders_table').dataTable( {
"bJQueryUI": true,
"bProcessing": true,
"bServerSide": true,
"aaSorting": [[3,'asc'],[4,'asc']],
"iDisplayLength": 100,
"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"sPaginationType": "full_numbers",
"sDom": 'T<"clear"><"fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix"lfr>t<"fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix"ip>',
"oTableTools": {
"sSwfPath": "media/copy_cvs_xls_pdf.swf",
"aButtons": [
"copy",
"print",
{
"sExtends": "collection",
"sButtonText": "Save",
"aButtons": [ "csv", "xls", "pdf" ]
}
]
},
"aoColumnDefs": [
{"bSortable": false, "aTargets": [0]},
{"bVisible": false, "aTargets": [1]},
{"sClass": "center", "aTargets": ["_all"]},
{
"fnRender": function (oObj) {
var sValue = oObj.aData[2];
var aDate = sValue.split(' ');
var fDate = aDate[0].split('-');
return fDate[1]+"/"+fDate[2]+"/"+fDate[0];
},
"bUseRendered": false,
"aTargets": [2]
},
{"sSortDataType": "orderstatus", "aTargets": [3]},
],
"sAjaxSource": "data/getData.php",
"fnInitComplete": function (){
}
});
// Row Details
$('#orders_table').on('click', 'tbody td img', function(){
var nTr = this.parentNode.parentNode;
if (this.src.match('details_close')){
/* This row is already open - close it */
this.src = "images/details_open.png";
oTable.fnClose(nTr);
} else {
/* Open this row */
this.src = "images/details_close.png";
oTable.fnOpen(nTr,fnFormatDetails(nTr),'details');
}
});
This discussion has been closed.
Replies
If that doesn't solve it, could you run your table through the DataTables debugger ( http://debug.datatables.net ) and let me know what the debug code is.
Allan