实际上,您的问题使我意识到分组小部件缺少此功能。因此,我在 2.12.0 版本中添加了两个新选项(新的分组小部件演示):
group_callback
- 允许您修改组标题文本的功能
group_complete
- 分组小部件完成后在表上触发的事件名称。
所以现在你可以做这样的事情(演示):
var total = 0,
targetColumn = 1,
$table = $('table');
$table.on('groupingComplete', function(){
// don't include the group header rows
$table.find('tbody tr:not(.group-header)').each(function(){
total += parseFloat( $(this).find('td').eq(targetColumn).text() );
});
$table.find('.group-header .total').html( total );
});
$table.tablesorter({
theme: 'blue',
widthFixed: true,
widgets: ['group'],
widgetOptions: {
// text added to the group header - {num} is the number of rows in that group
group_count: ' ({num})',
// add a group subtotal to the "Numeric" column
group_callback: function ($cell, $rows, column, table) {
// callback allowing modification of the group header labels
// $cell = current table cell (containing group header cells '.group-name' & '.group-count'
// $rows = all of the table rows for the current group; table = current table (DOM)
// column = current column being sorted/grouped
if (column === targetColumn) {
var t, subtotal = 0;
$rows.each(function () {
subtotal += parseInt($(this).find('td').eq(column).text());
});
t = '; <span class="subtotal">' + subtotal +
'</span>/<span class="total"></span>';
$cell.find('.group-count').append(t);
}
}
}
});
如果您需要定位多个列,您可以更改targetColumn
为数组,然后使用targetColumn.indexOf(column) >= 0
代替column === targetColumn
.