I'm using JQuery Tablesorter version 2.17.8 to sort a table
I've created a custom parser to sort a date, followed by a time, followed by an optional string.
The parser looks like this:
$.tablesorter.addParser({
id: "datetime",
is: function (s) {
return /\d{2}\-\d{2}\-\d{2}\s\d{2}:\d{2}(door \w+)?/.test(s);
}, format: function (s) {
arr = s.split(/[\s\-:]/);
if(arr[0] == "") // table column is empty
timestamp = null;
else {
// Month is zero-indexed so subtract one from the month inside the constructor
date = new Date(Date.UTC('20' + arr[2], arr[1] - 1, arr[0], arr[3], arr[4])); // Y M D H M
timestamp = date.getTime();
}
console.log(timestamp);
return timestamp;
}, type: "numeric"
});
Example data of the table column to be sorted:
The parser works as intended but I want the empty cells to be sorted to the bottom and that doesn't seem to work.
I placed this in my PHP file:
<script type="text/javascript">
{literal}
$(document).ready(function()
{
$("#betalingen").tablesorter( {
headers: {
5 : { sorter: false } }, // disable sorting for column #5
widgets: ["saveSort", "zebra"], // apply alternating row coloring
sortList: [[2,0]], // initial sorting order for Date column = ascending (0)
emptyTo: 'bottom' // empty cells are always at the bottom
}
);
} );
{/literal}
</script>
Note that I've tried every option here: http://mottie.github.io/tablesorter/docs/example-option-sort-empty.html
And when I remove my custom parser (and let Tablesorter figure out a parser), it sorts the empty cells to the bottom as intended but obviously the column isn't sorted correctly.
Anyone knows what is going on here?
You'll need to tweak you parser slightly.
is
function since you can set the parser to a column and not need to auto-detect it.As @karlingen pointed out, when the cell is empty, you're still returning timestamp
which has been set to null; it gets saved as null and not an empty string.
$.tablesorter.addParser({
id: "datetime",
is: function (s) {
// no need to auto-detect
return false;
},
format: function (s) {
var date, timestamp,
arr = s.split(/[\s\-:]/);
if (arr[0] !== "") {
// Month is zero-indexed so subtract one from the month inside the constructor
date = new Date(Date.UTC('20' + arr[2], arr[1] - 1, arr[0], arr[3], arr[4])); // Y M D H M
timestamp = date.getTime();
}
// is date really a date?
return date instanceof Date && isFinite(date) ? timestamp : s;
},
type: "numeric"
});