Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  167] [ 5]  / answers: 1 / hits: 17208  / 7 Years ago, thu, april 13, 2017, 12:00:00

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?


More From » datatable

 Answers
26

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.


[#58162] Tuesday, April 11, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;