jQuery DataTables Filtering for Specific Columns Only

主宰稳场 提交于 2019-11-29 23:32:15

You can modify your selector to ignore the first <td> element. The index of each matched element should be 1 less than the corresponding column index.

/* Add a select menu for each TH element except the first in the table footer */
$("thead #filter td:not(:eq(0))").each( function ( i ) {
    var columnIndex = i + 1;
    this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(columnIndex) );
    $('select', this).change( function () {
        oTable.fnFilter( $(this).val(), columnIndex );
    } );
});

If you wanted to be able to specify the column indexes for which you wanted a filter, one way would be to do something like

var filterIndexes = [3, 4];
$('td', '#filter').each( function ( i ) {
    if ($.inArray(i, filterIndexes) != -1) {
        this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
        $('select', this).change( function () {
            oTable.fnFilter( $(this).val(), i );
        });
    }
});

Or, if you wanted to control filters by adding a class .filter to any <th> element whose column you wanted to filter, you could do something like

$('th', '#labels').each( function(i) {
    if ($(this).hasClass( 'filter' )) {
        $('td', '#filter').eq(i)
          .html( fnCreateSelect(oTable.fnGetColumnData(i)) )
          .find('select')
          .change(function () { oTable.fnFilter($(this).val(), i); });
    }
});    

Not tested, but you get the idea :)

In the current version according to http://www.datatables.net/examples/api/multi_filter_select.html, for one column, I got lucky with this.api().column(0).every:

$(document).ready(function() {
    $('#mytable').DataTable( {
        initComplete: function () {
            this.api().column(0).every( function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.header()).empty() )
                    .on( 'change', function () {
                        var 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>' )
                } );
            } );
        },
    } );
} );

But is "every" still needed then? And how to target multiple columns?

I seconded matt voda answer, use this code for the first column:

this.api().column( [0] ).every 

and use this code for more:

this.api().column( [0,1,2] ).every 

On the tfoot column which you want to skip, you can leave it blank

The easiest way is put this for unneeded columns:

<input type="hidden">
Avinash Garg

for basic search you can get idea from:

$('tbody tr').addClass('visible');  


$('thead tr th input:text').keyup(function(event) {      



  if (event.keyCode == 27 || $(this).val() == '')
      {  ``$('tbody tr td ').show();
     }
  }
Avtandil Kavrelishvili
$("tfoot th").each( function ( i ) {
  if(i>=3 && i<=6)
    this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
  $('select', this).change( function () {
    oTable.fnFilter( $(this).val(), i );
  });
});

This is the best way for insert Filter for Specific Column.

i - Is Number Of Coulmn.

<th></th> - *Tags number must be equal of Columns.

There is even a very easy way: for those columns you don't want filtering just set the visibility to hidden of <th> in <tfoot> Example:

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