DataTable search() multiple columns and values with OR
DataTable search() multiple columns and values with OR

Hello,
I have this Datatable where I need to filter some columns but making like and OR with other specific column, the idea is when this column have a value (true for instance) no matter if the other filter don't match rows for that column, that rows should be present in the grid, lets say I have:
ID | col1 | col2 | col3
1 | true | val1 | false
2 | false | val2 | true
3 | false | val1 | false
Each column will have its own filter, then when a user choose to filter by "col1 = true" normal behavior will show only Row with ID = 1 (only row where col1 = true), but in my scenario because in row with ID = 2 "col3 = true" my Grid should show rows with ID = 1 and 2.
Is like "it doesn't matter what filters are, if col3 = true show those rows all the time".
Any idea for this? Things like: table.columns([1, 3]).search($(this).val()).draw();
...Are not working for me.
Thanks.
This question has an accepted answers - jump to answer
Answers
I would look at creating a search plugin for this. In the plugin you can first check to see if
col1 === true
and return true if so. Then check to see ifcol3 === true
and return true if so. Finally return false.I believe this is an AND search which is why its not working the way you want.
Kevin
Thanks Kevin will try to implement that solution.
Give it a shot and if you need help please post back. The best thing to do would be to provide a test case with your try so we can look live at your code for debugging.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Is there a way to know the name or index of the column where a user is trying to filter?
For instance if user try to filter "ID, col2 or col3" we want standard behavior, but when user filter "col1" then we want the plug-in takes action.
I'm trying to find something in these parameters but no luck so far:
$.fn.dataTable.ext.search.push(
function( settings, searchData, index, rowData, counter)
Thanks.
Not sure I understand the question. You will need to know the column index. Specifics on how to get that would be up to your solution and how you are allowing the user to create these searches. As described [here[(https://datatables.net/manual/plug-ins/search#Inputs) the second parameter (searchData) is an array of data for the row. Based on the above example you would use something like this for
col1
:The above code may need to be adjusted to work.
Kevin
That worked thanks, marking your answer as correct, really appreciate your help.