使用 tablesorter 插件,您应该参考有关编写自己的解析器的文档。在这里看到它:http: //tablesorter.com/docs/example-parsers.html
您所要求的似乎几乎完全符合文档中使用的示例。为方便起见,文档中的代码复制如下。
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
// set a unique id
id: 'grades',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0);
},
// set type, either numeric or text
type: 'numeric'
});
$(function() {
$("table").tablesorter({
headers: {
6: {
sorter:'grades'
}
}
});
});
出于您的目的,代码如下所示:
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
// set a unique id
id: 'medals',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
// Note the 'i' added after the medal type, that makes it case insensitive.
return s.toLowerCase().replace(/gold/i,2).replace(/silver/i,1).replace(/bronze/i,0);
},
// set type, either numeric or text
type: 'numeric'
});
$(function() {
$("table").tablesorter({
headers: {
6: { // Replace '6' with the number of the column that your medals are in
sorter:'medals'
}
}
});
});