我按照this example在我的数据表上添加了下拉过滤.当一个页面上只有一个表时,它工作得很好,但我有一个场景,其中有两个表.

根据DataTables文档(link 1link 2),API可以同时访问所有DataTables实例.

所以,我试着在每张桌子上

这api().表([0]).列([3,4]).every(函数(){

但过滤下拉列表仅出现在第一个表中.

 $('#table1').DataTable({
        "dom": '<"dtWrapper"<f>lrti<"paginate0rem"p>>',
        drawCallback: function(){
            if (feather) {
                feather.replace({
                    width: 14,
                    height: 14
                });
            }
        },
        orderCellsTop: true,
        initComplete: function () {
            这api().表([0]).列([3,4]).every(函数(){
                let column = this,
                    select = $('<select class="form-control"><option value=""></option></select>')
                        .appendTo($('thead tr:eq(1) th:eq(' + this.index()  + ')'))
                        .on('change', function () {
                            let val = $.fn.dataTable.util.escapeRegex(
                                $(this).val()
                            );

                            column
                                .search( val ? '^'+val+'$' : '', true, false)
                                .draw();
                        });

                column.data().unique().sort().each(function (d, j) {
                    select.append('<option value="'+d+'">'+d+'</option>')
                });
            });
        }
    });

    $('#table2').DataTable({
        "dom": '<"dtWrapper"<f>lrti<"paginate0rem"p>>',
        drawCallback: function(){
            if (feather) {
                feather.replace({
                    width: 14,
                    height: 14
                });
            }
        },
        orderCellsTop: true,
        initComplete: function () {
            this.api().tables([1]).columns([2, 3]).every( function () {
                let column = this,
                    select = $('<select class="form-control"><option value=""></option></select>')
                        .appendTo($('thead tr:eq(1) th:eq(' + this.index()  + ')'))
                        .on('change', function () {
                            let val = $.fn.dataTable.util.escapeRegex(
                                $(this).val()
                            );

                            column
                                .search( val ? '^'+val+'$' : '', true, false)
                                .draw();
                        });

                column.data().unique().sort().each(function (d, j) {
                    select.append('<option value="'+d+'">'+d+'</option>')
                });
            });
        }
    });

推荐答案

  1. 正如你会注意到的那样,第二张桌子上打this.api().tables([0])似乎不起作用.如果在一个 Select 器中使用两个表来创建数据表(例如$("#table1, #table2").DataTables()),我怀疑这会起作用.因此,将其更改为使用所需表的实际 Select 器(例如$("#table1").DataTable().columns([2]))效果更好.可能有一种方法可以获取"上下文"数据表,而无需使用 Select 器.但这应该让你朝着正确的方向前进.

  2. 您的.appendTo()调用试图 Select html中的所有thead tr:eq(1).因此,如果您已经开始了第一步,那么这个损坏的附加将导致问题.

查看带有以下修复程序的演示:

$(document).ready(function() {
  $('#table1').DataTable({
    "dom": '<"dtWrapper"<f>lrti<"paginate0rem"p>>',
    orderCellsTop: true,
    initComplete: function() {
      var $table = $("#table1");
      $table.DataTable().columns([2]).every(function() {
        let column = this;
        let select = $('<select class="form-control"><option value=""></option></select>')
          .appendTo($table.find('thead tr:eq(1) th:eq(' + this.index() + ')'))
          .on('change', function() {
            let val = $.fn.dataTable.util.escapeRegex($(this).val());

            column
              .search(val ? '^' + val + '$' : '', true, false)
              .draw();
          });

        column.data().unique().sort().each(function(d, j) {
          select.append('<option value="' + d + '">' + d + '</option>')
        });
      });
    }
  });

  $('#table2').DataTable({
    "dom": '<"dtWrapper"<f>lrti<"paginate0rem"p>>',
    orderCellsTop: true,
    initComplete: function() {
      var $table = $("#table2");

      $table.DataTable().columns([3]).every(function() {
        let column = this;
        let select = $('<select class="form-control"><option value=""></option></select>')
          .appendTo($table.find('thead tr:eq(1) th:eq(' + this.index() + ')'))
          .on('change', function() {
            let val = $.fn.dataTable.util.escapeRegex(
              $(this).val()
            );

            column
              .search(val ? '^' + val + '$' : '', true, false)
              .draw();
          });

        column.data().unique().sort().each(function(d, j) {
          select.append('<option value="' + d + '">' + d + '</option>')
        });
      });
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>



<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">

<body>
  <div>
    <table id="table1">
      <thead>
        <tr>
          <th>header 1</th>
          <th>header 2</th>
          <th>header 3</th>
          <th>header 4</th>
        </tr>
        <tr>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>col 1</td>
          <td>col 2</td>
          <td>col 3</td>
          <td>col 4</td>
        </tr>
        <tr>
          <td>col 1</td>
          <td>col 2</td>
          <td>col 3</td>
          <td>col 4</td>
        </tr>
      </tbody>
    </table>

    <table id="table2">
      <thead>
        <tr>
          <th>header 1</th>
          <th>header 2</th>
          <th>header 3</th>
          <th>header 4</th>
        </tr>
        <tr>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>col 1</td>
          <td>col 2</td>
          <td>col 3</td>
          <td>col 4</td>
        </tr>
        <tr>
          <td>col 1</td>
          <td>col 2</td>
          <td>col 3</td>
          <td>col 4</td>
        </tr>
      </tbody>
    </table>
  </div>

</body>
</html>

Jquery相关问答推荐

jQuery从JSON创建嵌套列表

JQuery AJAX 成功事件未触发

Visual Studio - 将文本粘贴到 cshtml 中会删除文本

Ajax 替换而不是追加

在 Bootstrap 3 中向工具提示添加换行符

jQuery 1.4.1 中缺少 JSON 字符串化?

$(document).ready(function() VS $(function(){

在 jQuery 中构建 html 元素的最清晰方法

使用 jQuery 按文本内容 Select 选项

使用 jQuery 更改 CSS 类属性

在 `click` 和 `enter` 上触发事件

使用 jQuery 将行添加到表的 tbody

jQuery.parseJSON 与 JSON.parse

Backbone.js 和 jQuery

使用 JQuery 激活 bootstrap 选项卡

jquery更改div类的样式属性

如何获取第一个元素而不是在 jQuery 中使用 [0]?

Twitter Bootstrap alert 消息关闭并再次打开

为什么我应该创建异步 WebAPI 操作而不是同步操作?

jQuery append() 与 appendChild()