Special sort for 1 column

Special sort for 1 column

RoyHBRoyHB Posts: 4Questions: 0Answers: 0
edited May 2013 in DataTables 1.9
I've just started using DataTables - very nice and useful work.

I have a problem that I can't quite figure out the solution to.
In my table there is one column that requires some special handling when sorting. The column has a class named 'Q'.

I initialise dataTables:
[code]
$("#tableLeft").dataTable({
"aLengthMenu": [[35, 70, -1], [35, 70, "All"]],
"iDisplayLength":"35",
"aoColumnDefs": [{ "sSortDataType": "qpos", "aTargets": [ "Q" ] }]
});
[/code]

My expectation is that this will cause plugin's named qpos-asc or qpos-desc will be called as the sort callbacks when the column has the class "Q" and it's clicked.

The purpose of the special sort is to cause any entries that are not numeric or have numeric values < 1 to be sorted to the bottom of the table for an asc sort and the top of the table for a desc sort.

The qpos functions I have are:
[code]
$.fn.dataTableExt.afnSortData['qpos-asc'] = function(a,b) {
var badAssign = 9999;

if ($.isNumeric(a["queue_position"]) && a["queue_position"] > 0) { aItem = a["queue_position"]; }
else { aItem = badAssign; }

if ($.isNumeric(b["queue_position"]) && b["queue_position"] > 0) { bItem = b["queue_position"]; }
else { bItem = badAssign; }

return ((aItem < bItem) ? -1 : ((aItem > bItem) ? 1 : 0));

};
$.fn.dataTableExt.afnSortData['qpos-desc'] = function(a,b) {
var badAssign = 9999;

if ($.isNumeric(a["queue_position"]) && a["queue_position"] > 0) { aItem = a["queue_position"]; }
else { aItem = badAssign; }

if ($.isNumeric(b["queue_position"]) && b["queue_position"] > 0) { bItem = b["queue_position"]; }
else { bItem = badAssign; }

return ((aItem < bItem) ? 1 : ((aItem > bItem) ? -1 : 0));
};

[/code]

It appears that the sort functions qpos-asc and qpos-desc are never called.

I'm sure that I've made some irritating newbie error.

Help would be most appreciated

Roy

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I think you want to use a sorting function like this, rather than an afnSortData function: http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html .

    Allan
  • RoyHBRoyHB Posts: 4Questions: 0Answers: 0
    Thanks Allan - you put me on the right track. I also discovered an error on my part - I forgot that dataTables would pass me the cell content rather than the object the content was drawn from.

    Once I changed the methodology and fixed my bug all works well
This discussion has been closed.