当前位置: 首页 > 知识库问答 >
问题:

Tablesorter筛选器和求和问题

蓬祺
2023-03-14

我有一个带有tablesorter的表,在其中我希望有十进制列的总和,还希望有可能按任何列筛选数据。

当我按小数筛选表时,它工作得很好,但当我按日期筛选表时,带有总计的行就会消失。

$($('#incomesTable').tablesorter({
  theme: 'bootstrap',
  sortInitialOrder: "desc",
  widgets: ['math', 'zebra', 'filter'],
  widgetOptions: {
    filter_cssFilter: '',
    filter_childRows: false,
    filter_hideFilters: false,
    filter_ignoreCase: true,
    filter_saveFilters: true,
    filter_searchDelay: 300,
    filter_startsWith: false,
    math_data: 'math',
    // math_ignore: [0, 1],
    math_none: 'N/A',
    math_complete: function($cell, wo, result, value, arry) {
      var txt = '<span class="align-decimal">' +
        result + '</span>';
      if ($cell.attr('data-math') === 'all-sum') {
        return txt + ' (Sum of ' + arry.length + ' cells)';
      }
      return txt;
    },
    math_completed: function(c) {
      console.log('math calculations complete', c.$table.find('[data-math="all-sum"]:first').text());
    },
    math_textAttr: '',
    math_mask: '0.##',
    math_prefix: '',
    math_suffix: '',
    math_event: 'recalculate',
    math_priority: ['row', 'above', 'below', 'col'],
    math_rowFilter: ''
  }
}));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/css/theme.bootstrap.min.css" integrity="sha512-1r2gsUynzocV5QbYgEwbcNGYQeQ4jgHUNZLl+PMr6o248376S3f9k8zmXvsKkU06wH0MrmQacKd0BjJ/kWeeng==" crossorigin="anonymous" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js" integrity="sha512-qzgd5cYSZcosqpzpn7zF2ZId8f/8CHmFKZ8j7mU4OUXTNRd5g+ZHBPsgKEwoqxCtdQvExE5LprwwPAgoicguNg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.widgets.min.js" integrity="sha512-dj/9K5GRIEZu+Igm9tC16XPOTz0RdPk9FGxfZxShWf65JJNU2TjbElGjuOo3EhwAJRPhJxwEJ5b+/Ouo+VqZdQ==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/widgets/widget-math.min.js" integrity="sha512-9bQAn0y1alBDFoH7VsUSKQd0ilHlbtsGkfjBDIR0iIerF0kvzUp/CfRubUkYT13Ithu0jZ3T+9vBC7FooHwWXQ==" crossorigin="anonymous"></script>

<table id="incomesTable" class="table table-sm table-striped table-hover tablesorter">
  <thead>
    <tr>
      <th>Date</th>
      <th>Date</th>
      <th>Decimal</th>
      <th>Decimal</th>
      <th>Decimal</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>01.10.2020</td>
      <td>01.10.2020</td>
      <td>100</td>
      <td>200</td>
      <td>300</td>
    </tr>
    <tr>
      <td>01.11.2020</td>
      <td>01.11.2020</td>
      <td>10</td>
      <td>20</td>
      <td>30</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <th colspan="2">Total</th>
      <th data-math="col-sum">col-sum</th>
      <th data-math="col-sum">col-sum</th>
      <th data-math="col-sum">col-sum</th>
    </tr>
  </tbody>
</table>

正在工作的JSFIDLE:https://jsfidle.net/rorymccrossan/ak29tupf/

共有1个答案

何玺
2023-03-14

这个问题是因为当您执行筛选器时,表管理器正在查找计算值。

在您的示例中,在第一个十进制字段中搜索10将显示计算出的行,因为它的值匹配,例如。110。但是,当您搜索日期时,该列的计算单元格只包含单词“Total”,因此不匹配,并且计算行被隐藏。

解决此问题的快速方法是将计算出的行移到TFoot元素中,将其从筛选中排除:

<tfoot>
  <tr>
    <th colspan="2">Total</th>
    <th data-math="col-sum">col-sum</th>
    <th data-math="col-sum">col-sum</th>
    <th data-math="col-sum">col-sum</th>
  </tr>
</tfoot>

工作实例

或者,在只包含计算行的TBody上放置一个类,并使用TableSorter的CSSInfoBlock属性从筛选中排除该内容:

$($('#incomesTable').tablesorter({
  cssInfoBlock: 'calc',
  // other settings...
});
<tbody class="calc">
  <tr>
    <th colspan="2">Total</th>
    <th data-math="col-sum">col-sum</th>
    <th data-math="col-sum">col-sum</th>
    <th data-math="col-sum">col-sum</th>
  </tr>
</tbody>

工作实例

 类似资料:
  • 此正则表达式正在寻找几种格式的社会安全号码(SSN),但它也忽略了明显无效的SSN,如123-45-6789或000-00-0000等。 此表达式应找到一个社会保险号: 包含任何非数字分隔符(即、) 它还应该按顺序捕捉9位数字,不带分隔符,但以空格为边界(即“text################################`) 此表达式将忽略以下社会保险号:在任何特定组中包含所有零(即,,

  • 试图从结构中获取项目列表,避免大量的for循环和if,所以我想使用 例如,lets具有以下结构: 我要创建: null List TestitemsFiltered=House1.FamilyList.Stream().FlatMap(f->f.PersonList.Stream().FlatMap(p->p.ItemList.Stream().Filter(Item->Item.Name.Equ

  • Tablesorter 是一个用来直接在浏览器上对表格数据进行排序的jQuery插件,无需再次刷新页面,支持多种单元格数据类型,例如数值、字符串、日期和自定义排序。 主要的特点包括: 多列排序 支持文本、URL地址、数值、IP地址、日期类型,以及自定义类型排序 支持 TH 元素的 ROWSPAN 和 COLSPAN 属性 支持第二个隐藏域排序 可扩展外观 程序简小,打包后只有 7.4K 程序效果:

  • 对不起,我仍然是小新在这方面,只是万一有代码有人想看到我否定这里是整个代码要点嵌入 我花了2个星期建立这个应用程序,并已卡在这个过滤在过去的2天。我需要有这个应用程序MVP明天下午演示在一个就业活动为最近的毕业生,这是仅剩的2件事之一。 我没有足够的代表赏金,但任何帮助或建议都很感激

  • 有什么建议吗?

  • 筛选器。 Usage 全部引入 import { Picker } from 'beeshell'; 按需引入 import Picker from 'beeshell/dist/components/Picker'; Examples Code import { Picker } from 'beeshell'; <Picker ref={(c) => { this._pick