I am using tablesorter for jquery and using this page as example
http://mottie.github.io/tablesorter/docs/example-parsers.html
and using this plugin:
http://mottie.github.io/tablesorter/js/jquery.tablesorter.js
this is the code to sort "status"
$.tablesorter.addParser({
id: 'status',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s, table, cell, cellIndex) {
// format your data for normalization
//console.log(cellIndex);
return s.toLowerCase()
.replace(/Deleted/,4)
.replace(/Finished/,3)
.replace(/Cancelled/,2)
.replace(/In Progress/,1)
.replace(/New/,0);
},
// set type, either numeric or text
type: 'numeric'
});
$('#request').tablesorter(
{
debug:false,
widthFixed: false,
headers: {
0: { sorter: false },
3: { sorter: false },
4: { sorter: 'dates-desired' },
6: { sorter: 'dates-projected' },
7: { sorter: 'status' },
8: { sorter: false },
}
}
);
But when i click the status thead, nothing happens
when i make the debug option: true, this is the o/p:
"Sorting on 7,0 and dir 0 time (1ms)" jquery.tablesorter.min.js:157:5
"Rebuilt table (4ms)" jquery.tablesorter.min.js:157:5 "Completed
applying 0 widgets (0ms)"
Any help appreciated.
The issue in within the parser code... the text is set to lower case, but the regex contains upper case characters. Try this updated parser:
var arry = [ 'new', 'in progress', 'cancelled', 'finished', 'deleted' ];
$.tablesorter.addParser({
id: 'status',
is: function() {
// return false so this parser is not auto detected
return false;
},
format: function( s, table, cell, cellIndex ) {
var str = ( s || '' ).toLowerCase(),
index = arry.indexOf( str );
// return original text if index not found; this allows proper
// sorting of empty cells
return index < 0 ? s : index;
},
// set type, either numeric or text
type: 'numeric'
});
If you need to support older versions of IE, then change from using indexOf
to jQuery's $.inArray()
function
index = $.inArray( str, arry );