I have data in column like "firstValue secondValue". This data must be in one column (I cant make two columns). When I set sorting, it sort by firstValue and secondValue. Now after some event I want to change to sort data by secondValue (ignore firstValue). After another event I want to sort by firstValue. Is this possible? How can I do that?
1 Answer
This is probably too late, but I just came upon the same problem. The solution involves a combination of custom sorting and altering the table settings in the event handler.
Some code to make it more concrete:
$.extend($.fn.dataTableExt.oSort, {
"secondValue-pre": function ( a ) {
var parts = a.split(' ')[1];
},
"secondValue-asc": function ( a, b ) { return ((a < b) ? -1 : ((a > b) ? 1 : 0)); },
"secondValue-desc": function ( a, b ) { return ((a < b) ? 1 : ((a > b) ? -1 : 0)); }
});
var $myTable = $('#example').dataTable({
"aoColumns": [
null, //your column containing "firstValue secondValue"
null,
]
});
$(document).ready(function() {
$('.something').click( function () {
var oSettings = $myTable.fnSettings();
oSettings.aoColumns[0].sType = 'secondValue';
});
});
I'm sure there are numerous ways to improve this code, but it should get you started.