Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables - Search in multiple columns with a single drop down

I'm working with DataTables and i'm trying to search a result in a table with a dropdown. But rather than searching one column, I need to search in two specific columns.

The below syntax works with a single column but how do i do it with multiple columns?

var table = $('#example1').DataTable();
    $("#filter").on('change', function() {
        table.column([4]).search($(this).val()).draw();
    });    

I tried doing this but when i use this code it only searches the result in the first column, E.g. 4th Column. and ignores the rest.

        table.column([4,5]).search($(this).val()).draw();

What is the proper method for this?

like image 252
Kasun Wijesekara Avatar asked Apr 13 '17 05:04

Kasun Wijesekara


Video Answer


4 Answers

Lets summarize all things here. It will help other people as well.

You can achieve this as follow:

table.column(4).search(this.value).column(5).search(this.val‌​ue).draw();

It will perform search on 4 column (4 is index of column), after that it will filter data from 5 column against provided filter value and at the end it will draw the table.

One thing to keep in mind is that Filter is applied on both columns, so both columns must contains matching data.

Here is its filddle


This can be achieved by using fnMultiFilter as it documentation explains:

This plug-in adds to DataTables the ability to set multiple column filtering terms in a single call (particularly useful if using server-side processing). Used in combination with the column sName parameter, simply pass in an object with the key/value pair being the column you wish to search on, and the value you wish to search for.

like image 94
mmushtaq Avatar answered Sep 21 '22 22:09

mmushtaq


Use columns() in place of column():

var table = $('#example1').DataTable();
$("#filter").on('change', function() {
    table.columns([4,5]).search($(this).val()).draw();
}); 
like image 45
Govind Samrow Avatar answered Sep 25 '22 22:09

Govind Samrow


From the doc, you should be using .columns() (note the plural)

like image 20
LeGEC Avatar answered Sep 21 '22 22:09

LeGEC


For an OR search among multiple columns, disable columns for the search using

columns.searchableSince: Enable or disable search on the data in a certain column.

Alternatively, you can also use an HTML attribute to remove the column from the search

<th data-searchable=false>
like image 32
fydelio Avatar answered Sep 23 '22 22:09

fydelio