javascript - 如何将搜索添加到页脚中 DataTable 的某些单独列?

标签 javascript html jquery datatables jquery-events

我需要将不同类型(文本框、下拉列表)的过滤添加到 DataTable 中的某些(!)单个列到页脚。也就是说,我希望能够通过单个列搜索我添加到页脚的每个过滤器,并且过滤器的类型将取决于列,例如,对于第 0 列,它是一个文本框,对于第 1 列,它是一个下拉列表,对于第 5 列,它是一个日期选择器。

这是一个测试示例。请注意构造函数的新类型(DataTable,而不是 dataTable)。

$("#my-table").DataTable({
  //.....
  , 'initComplete': function (settings, json) {
      var cl = this.api().columns(1); //2nd column

      $(cl.footer()).html("fdsfds"); //doesn't work

      //this.api().columns().every(function(){
        //var column = this;
        //$(column.footer()).html('fdsfsdfd');  //doesn't work either
      //});


      //neither this

      //var api = this.api();
      // $(api.column(1).footer()).html('dsfs2222');
  });

怎么了?

最佳答案

你需要在这里做两件事。

  • 在你的 table 上加一只脚,这样它就有空间可以加了
 <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th colspan="4" style="text-align:right">Total:</th>
                <th></th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td>Tiger</td>
                <td>Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>$320,800</td>
            </tr>
        </tbody>
</table>
  • 像此处指定的那样使用 footerCallBack http://datatables.net/examples/advanced_init/footer_callback.html您还使用了列而不是列。

     $(document).ready(function() {
          $('#example').DataTable({
          "footerCallback": function ( row, data, start, end, display ) {
              var api = this.api(), data;
              $(api.column(1).footer()).html("test text");
           }
          });
      });
    

关于javascript - 如何将搜索添加到页脚中 DataTable 的某些单独列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821797/

相关文章:

javascript - 单击页面中任意位置时隐藏菜单

javascript - 如何在 td 内部重复数组,以便 td 包含所有数组值

javascript - 如何使用 selenium 在 Code Mirror 行中输入文本?

javascript - 如何使用 jquery-footable 创建过滤器下拉列表?

javascript - 具有多种方法的 jQuery 插件

javascript - 解析 window.location.pathname 并返回 number of/s 的正则表达式

javascript - 选择一个元素而不选择父元素

javascript - 如何使用 Polymer 在其他浏览器中启用 HTML 导入?

javascript - 如何在动画之前延迟 .css 函数

javascript - Bootstrap 缩略图不起作用