How to add search to some individual columns of DataTable in the footer?

ⅰ亾dé卋堺 提交于 2019-12-24 00:59:19

问题


I need to add filtering of different types (textbox, dropdown) to some(!) individual columns in DataTable to the footer. That is, I want to be able to search by a single column for each filter I add to the footer and the type of the filter will depend on a column, say, for the column 0 it's a textbox, for the column 1 it's a dropdown, for the column 5 it's a datepicker.

Here's a test example. Note the new type of the constructor (DataTable, not 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');
  });

What's the matter?


回答1:


You need to do two things here.

  • Add a tfoot to your table so it will have a space to add it
 <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>
  • Use footerCallBack like specified here http://datatables.net/examples/advanced_init/footer_callback.html You also used columns instead of column.

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


来源:https://stackoverflow.com/questions/35821797/how-to-add-search-to-some-individual-columns-of-datatable-in-the-footer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!