Kasun Wijesekara
Kasun Wijesekara

Reputation: 167

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?

Upvotes: 8

Views: 18656

Answers (4)

fydelio
fydelio

Reputation: 953

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>

Upvotes: 1

Govind Samrow
Govind Samrow

Reputation: 10179

Use columns() in place of column():

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

Upvotes: 5

mmushtaq
mmushtaq

Reputation: 3520

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.

Upvotes: 8

LeGEC
LeGEC

Reputation: 51790

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

Upvotes: 1

Related Questions