【问题标题】:datatable change cell value if filter criteria is not matching instead of hiding row如果过滤条件不匹配而不是隐藏行,则数据表更改单元格值
【发布时间】:2016-01-25 12:17:13
【问题描述】:

我需要帮助。我正在使用数据表过滤器,如果未找到记录并且需要隐藏行,而不是在记录不符合条件的情况下隐藏行显示单元格(带有 - 值),则此处的列过滤器会隐藏行。让我知道是否可以提供更多详细信息。

【问题讨论】:

  • 提供更多信息,例如您正在运行的查询,您想要什么样的输出。
  • 所以过滤后,要保持相同的行数,但是td的内容必须换成'-'?
  • @P.Jairaj 是的,请参阅此链接。 chenjidesigns.com/practut/performance.html。这里我有多个带有分数的列,如果条件不匹配,只有内容将替换为“-”。
  • @markpsmith 是的,当过滤器被清除或匹配时,它应该显示数据。页脚将相应更新。我已经完成了页脚回调。请参阅此链接进行设计。 chenjidesigns.com/practut/performance.html
  • 您使用的是服务器端数据吗?

标签: jquery datatables datatables-1.10


【解决方案1】:

由于您没有提供有关用于显示信息的数据结构的任何信息,因此我假设该结构是以下代码中的对象 data。 html 代码中的按钮在单击时会更改条件。

希望对你有帮助!!

为数据编辑

$(document).ready(function() {
  var data = [
    ["name1", 24],
    ["name2", 54]
  ];
  var crit = 0;
  $("#crit1").click(function() {
    crit = 20;
    filter();
  });
  $("#crit2").click(function() {
    crit = 40;
    filter();
  });
  var table = $('#example').DataTable({
      "data": data,
      "columns": [{
        title: "Name"
      }, {
        title: "Score"
      }],
      "columnDefs": [{
        // The `data` parameter refers to the data for the cell (defined by the
        // `data` option, which defaults to the column being worked with, in
        // this case `data: 0`.
        "render": function(data, type, row) {
          if (data > crit)
            return data;
          else
            return "-";
        },
        "targets": 1
      }]
    });
  var filter = function() {
    table.clear().draw();
    table.rows.add(data); // Add new data
    table.columns.adjust().draw();
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet" />

<input type="button" id="crit1" value="20%" />
<input type="button" id="crit2" value="40%" />
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Score</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>Name</th>
      <th>Score</th>
    </tr>
  </tfoot>
  <tbody>

  </tbody>
</table>

有用的链接:

https://www.datatables.net/examples/data_sources/js_array.html

https://www.datatables.net/examples/advanced_init/column_render.html

【讨论】:

  • 数据结构相同,每个按钮都有一个常量值,在范围内我分别为单元格分配常量值,是否有任何数据表功能可用于此过滤器。由于我可能需要分页和页脚计算,我认为它会数据表将减少我的代码并且易于处理。
猜你喜欢
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 2019-04-17
  • 2021-04-16
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多