gpt4 book ai didi

jquery - tablesorter自定义显示组标题

转载 作者:行者123 更新时间:2023-12-01 08:01:38 25 4
gpt4 key购买 nike

首先,我使用 TableSorter 2.11.1 和 jquery 1.9.1。

我使用此演示对我的行进行分组 ( http://mottie.github.io/tablesorter/docs/example-widget-grouping.html )我按日期对表格进行排序,然后按周对其进行分组,如下图所示

enter image description here

我想要的是在分组栏中添加本周的总价格。比如一周的行数。

我在 widget-grouping.js 中搜索,找到了计算一周行数的代码

$tr = c.$table.find('tr.group-header').bind('selectstart', false);
if (wo.group_count) {
$tr.each(function(){
$(this).find('.group-count').html( wo.group_count.replace(/\{num\}/g, $(this).nextUntil('tr.group-header').filter(':visible').length) );
});
}

我不是 jquery 专家,所以我找不到获取价格并将其添加到组 header 的方法。

最佳答案

实际上你的问题让我意识到分组小部件缺乏此功能。因此,我在 2.12.0 版本中添加了两个新选项(新 grouping widget demo ):

  • group_callback - 允许您修改组标题文本的函数
  • group_complete - 分组小部件完成后在表格上触发的事件名称。

现在你可以做这样的事情(demo):

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 代替 列 === targetColumn.

关于jquery - tablesorter自定义显示组标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19446400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com