Sort A Mixed Data Column With Tablesorter
I'm currently using the JQuery Tablesorter plugin found here: http://www.tablesorter.com, and I'm having trouble with a column that contains both dates and text. Here's the jsfiddl
Solution 1:
It looks like you're trying to set the sorter type using meta data
<thclass="{sorter: 'text'}">first name</th>
But the metadata plugin wasn't loaded in that demo. So either load the metadata plugin, or add the sorter type to the header option:
$("table").tablesorter({headers: {
0: { sorter:"text" },
4: { sorter:"percent" }
}
});
Here's an updated demo.
Solution 2:
Found a not so great answer, but that works for now.
I removed this parser:
ts.addParser({
id: "shortDate",
is: function (s) {
return/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/.test(s);
}, format: function (s, table) {
var c = table.config;
s = s.replace(/\-/g, "/");
if (c.dateFormat == "us") {
// reformat the string in ISO format
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$1/$2");
} elseif (c.dateFormat == "uk") {
// reformat the string in ISO format
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
} elseif (c.dateFormat == "dd/mm/yy" || c.dateFormat == "dd-mm-yy") {
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/, "$1/$2/$3");
}
return $.tablesorter.formatFloat(newDate(s).getTime());
}, type: "numeric"
});
which seems to fix my issue, since it stops the date parser from executing altogether. This would probably cause issues in other scenarios, but seems to work for my page for now. I'm still open to any other answers, if people have them.
Post a Comment for "Sort A Mixed Data Column With Tablesorter"