问题
I'm currently working with a tablesorter table in which I need to use a custom parser for each header. I'd like to know if there's an easy way to do this, such as:
table.tablesorter({
headers: {
0-20: {
sorter:'CareerLast'
},
}
});
I know that the above code doesn't work, but I'm just wondering if there's a more readable way of applying the custom parser, other than manually placing it on each column by index.
回答1:
Well, I think you have three choices:
Define each header, 0 through 20 in the initialization options.
header : { 0 : { sorter : 'CareerLast' }, 1 : { sorter : 'CareerLast' }, 2 : { sorter : 'CareerLast' }, // etc 20 : { sorter : 'CareerLast' } }Use the meta data plugin and add the sorter definition in the header class:
// untested, but I think this will work $('table').find('thead th').addClass("{sorter:'CareerLast'}"); $('table').tablesorter();Try out my forked version of tablesorter and just add the sorter as a class name
$('table').find('thead th').addClass('sorter-CareerLast'); $('table').tablesorter();
回答2:
return true from 'is' in addParser
eg: this parser assigns 'N/A' a value of -1
$.tablesorter.addParser({
id: 'num-with-na',
is: function(s) {
//always use this
return true;
},
format: function(n){
return n === 'N/A' ? -1 : n;
},
type: 'numeric'
});
来源:https://stackoverflow.com/questions/9014962/jquery-tablesorter-custom-parser-to-all-headers