【问题标题】:tablesorter custom display group headertablesorter 自定义显示组标题
【发布时间】:2016-06-07 06:30:02
【问题描述】:

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

我使用这个演示来分组我的行 (http://mottie.github.io/tablesorter/docs/example-widget-grouping.html) 我按日期对表格进行排序,然后像这张图片一样按周对表格进行分组

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

我在 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 方面的专家,所以我找不到获取价格并将其添加到组标题的方法。

【问题讨论】:

  • 你能创建jsfiddle吗
  • jsfiddle.net/pZcY7 这里是 jsfiddle,但我复制了我的代码,但我的工作不如在我的电脑上好:/这是我第一次使用这个工具。

标签: jquery grouping tablesorter


【解决方案1】:

实际上,您的问题让我意识到分组小部件缺少此功能。所以,我在 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) &gt;= 0 而不是column === targetColumn

【讨论】:

  • 感谢您提供此新功能。这正是我想要的。真是好作品!
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 2012-08-18
  • 2020-06-29
  • 2014-01-18
  • 2020-11-23
  • 2021-09-21
  • 2016-11-21
  • 2015-01-07
相关资源
最近更新 更多