如果您使用的是tablesorter gem,我相信您可能正在使用我的tablesorter分支。
我有一个带有解析器的演示,该解析器使用复选框来更改行颜色。它还使用复选框状态更新缓存,以便您可以对该列进行排序。
此解析器不适用于原始版本的 tablesorter。
// add parser through the tablesorter addParser method
$.tablesorter.addParser({
id: 'checkbox',
is: function(s) {
return false;
},
format: function(s, table, cell, cellIndex) {
var $t = $(table), $c = $(cell), c,
// resort the table after the checkbox status has changed
resort = false;
if (!$t.hasClass('hasCheckbox')) {
$t
.addClass('hasCheckbox')
// make checkbox in header set all others
.find('thead th:eq(' + cellIndex + ') input[type=checkbox]')
.bind('change', function(){
c = this.checked;
$t.find('tbody tr td:nth-child(' + (cellIndex + 1) + ') input').each(function(){
this.checked = c;
$(this).trigger('change');
});
})
.bind('mouseup', function(){
return false;
});
$t.find('tbody tr').each(function(){
$(this).find('td:eq(' + cellIndex + ')').find('input[type=checkbox]').bind('change', function(){
$t.trigger('updateCell', [$(this).closest('td')[0], resort]);
});
});
}
// return 1 for true, 2 for false, so true sorts before false
c = ($c.find('input[type=checkbox]')[0].checked) ? 1 : 2;
$c.closest('tr')[ c === 1 ? 'addClass' : 'removeClass' ]('checked');
return c;
},
type: 'numeric'
});