How can I search multiple columns in DataTables?

前端 未结 1 1288
别跟我提以往
别跟我提以往 2020-12-03 15:47

I am trying to combine a column search and a normal search on specific columns.

If you look for example at the snippet below, I want to be able to search the columns

1条回答
  •  情深已故
    2020-12-03 16:24

    You can override the built in search / filter with a custom filter :

    $('.dataTables_filter input').unbind().on('keyup', function() {
        var searchTerm = this.value.toLowerCase();
        $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
           //search only in column 1 and 2
           if (~data[0].toLowerCase().indexOf(searchTerm)) return true;
           if (~data[1].toLowerCase().indexOf(searchTerm)) return true;
           return false;
       })
       table.draw(); 
       $.fn.dataTable.ext.search.pop();
    })
    

    By this the "normal search" only applies to the first two columns, Name and Position - and you still have working select boxes in the footer. Forked fiddle -> https://jsfiddle.net/g9gLjtjh/

    0 讨论(0)
提交回复
热议问题