Mario
Mario

Reputation: 185

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

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?

Upvotes: 2

Views: 994

Answers (1)

misha130
misha130

Reputation: 5706

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");
           }
          });
      });
    

Upvotes: 1

Related Questions