Search by Multiple Values in One Column

Search by Multiple Values in One Column

wallabe123wallabe123 Posts: 24Questions: 13Answers: 0

I am using Chart.js alongside DataTables so that when a section of a pie chart is selected, it filters the DataTable by that value. I want the user to be able to select multiple data sections and the values to be applied to the table at the same time as column filters.
Is there a way to either add to the current search for the same column or to clear the search and then search by multiple values at once.
E.g. 'apple' is already applied to the column as a filter. The user then selects 'orange'. The table should now show rows containing the 'apple' value or the 'orange' value in the same column.
I have it applying the filter for one value as below:

            label = chart.selectedValue();
            table.columns( 3 ).search(label).draw();

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    Answer ✓

    You will want to use regex mode. See the search() docs for details. You will want to use | as an OR operator between the search terms. This example might help:
    http://live.datatables.net/vipifute/1/edit

    NOTE: As of writing this it seems that the http://live.datatables.net environment is down. Try again later.

    Kevin

  • wallabe123wallabe123 Posts: 24Questions: 13Answers: 0

    @kthorngren thanks for the help! I got it working with the below Regex to include the whitespace in some of the filtering options:

        allFilters = currentFilters.join('|').replace("&", "\\&").replace(/\s/g, "\\s");
        table.columns(columnIndex).search(allFilters, true).draw();
    
Sign In or Register to comment.