简体   繁体   English

如何设置默认过滤器值和过滤器条目数据表

[英]How to set default filter values and filter entries datatable

I have the following scenario: 我有以下情况:

A user can visit a site with a DataTable in two ways: 用户可以通过两种方式访问​​带有DataTable的站点:

  1. Normal, no filter set, you'll see every entry. 正常,没有过滤器设置,您会看到每个条目。
  2. User should see only entries with predefined filters 用户应仅看到具有预定义过滤器的条目

I've accomplished so far to set the filter's value as well as the column's search value by setting them in the initComplete-method, but the DataTable doesn't updates after setting the filters, which is mysterious to me since the DataTable knows after pressing F5 the set filter value... 到目前为止,我已经通过在initComplete方法中设置过滤器的值以及列的搜索值来完成设置,但是设置过滤器后DataTable不会更新,这对我来说还是很神秘的,因为DataTable在按下后就知道了F5设定的过滤值...

So my question is: How do I get the desired result of setting default filter values, and tell the DataTable to update, after it finished it's initialization? 所以我的问题是:如何完成设置默认过滤器值的预期结果,并告诉DataTable在完成初始化后进行更新?

My DataTable-Initialization looks like this: 我的DataTable初始化看起来像这样:

$('#table-data').DataTable({
  processing: true,
  serverSide: true,
  language: dataTablesGerman,
  initComplete: function () {
    this.api().columns().every(initColumnFilter);
    const json = JSON.parse($('[name="table_settings"]').val());
    const dt = this.api();
    dt.columns().every(function (idx) {
      const column = this;
      if (json[idx] !== null) {
        const $current = $('input:not(.datetimerange), select', this.footer());
        const value = json[idx].search;
        $current.val(value);
        column.search(value).draw();
      }
    });
  }
});

The settings are looking like this (these settings are from the PHP Response, which are stored on a hidden field, the $('[name="table_settings"]').val() line): 设置看起来像这样(这些设置来自PHP响应,存储在隐藏字段$('[name="table_settings"]').val()行中):

const settings = [
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    { search: 1 } // this will be set to a select box
];

After some other experiments we could get it work with drawing the column again with a delay of 1ms. 经过一些其他实验,我们可以使其延迟1ms再次绘制列。 This made the DataTable apply the filter. 这使DataTable应用了过滤器。

So the code now looks like this: 因此,代码现在看起来像这样:

$('#table-data').DataTable({
  processing: true,
  serverSide: true,
  language: dataTablesGerman,
  initComplete: function () {
    this.api().columns().every(initColumnFilter);
    const json = JSON.parse($('[name="table_settings"]').val());
    const dt = this.api();
    if (json !== null && json.length > 0) {
      dt.columns().every(function (idx) {
        const column = this;
        if (json[idx] !== null) {
          const $current = $('input:not(.datetimerange), select', this.footer());
          const value = json[idx].search;
          $current.val(value);
          column.search(value).draw();
          window.setTimeout(function () {
            column.draw();
          }, 1);
        }
      });
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM