DataTable search() multiple columns and values with OR

DataTable search() multiple columns and values with OR

Erik_QuinteroErik_Quintero Posts: 4Questions: 1Answers: 0

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

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    Answer ✓

    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 if col3 === true and return true if so. Finally return false.

    table.columns([1, 3]).search($(this).val()).draw();
    

    I believe this is an AND search which is why its not working the way you want.

    Kevin

  • Erik_QuinteroErik_Quintero Posts: 4Questions: 1Answers: 0

    Thanks Kevin will try to implement that solution.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    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

  • Erik_QuinteroErik_Quintero Posts: 4Questions: 1Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited May 2019

    Is there a way to know the name or index of the column where a user is trying to filter?

    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:

    if (searchData[1] === true) {
      return true;
    }
    
    if (searchData[3] === true) {
      return true;
    }
    
    return false;
    

    The above code may need to be adjusted to work.

    Kevin

  • Erik_QuinteroErik_Quintero Posts: 4Questions: 1Answers: 0

    That worked thanks, marking your answer as correct, really appreciate your help.

This discussion has been closed.