0

I'm working with DataTables, the plug-in for the jQuery Javascript library. There are already possibilities to search and filter but I haven't found, what I'm looking for.

I already searched the web but I haven't found a solution to my question yet. Maybe I searched for the wrong terms, so I'm sorry if the solution to my problem might be very simple.

Example-table: Example-Table

So, what I want to do is the following:

  • provide ONE single dropdown with several filter options
  • every option provides a custom filter (e.g. 'option1' shows all records that are available, 'option2' shows all records that are available and have label F, 'option3' shows all records that are available and have label E, …)
  • apply the filter immediately 'live'

Summary: I want to create a dropdown that provides options which do a multiple column filtering/search.

So, how can I achieve this? Is this even possible in DataTables?

Thanks in advance.

2
  • Should be possible but please show us what you've tried already... perhaps in a simple JSFiddle? Commented Sep 5, 2016 at 11:54
  • I haven't found any solution for myself so far. Here's a little JSFiddle. I only achieved to search through the searchbar, no real filtering. And I know that I can search specific columns. But I don't know how to search multiple columns. Commented Sep 5, 2016 at 12:55

1 Answer 1

2

This JS:

$(document).ready(function() {
    var table = $('#example').DataTable();
    $('select[name="filter"]').change(function() {
        if (!$(this).val()) {
            table.columns().search("").draw();
        } else {
            table.columns().search("");
            var option = $(this).find(":selected");
            var columns = Object.keys(option.data());
            console.log(columns)
            $.each(columns, function(k, v){
                table.columns(parseInt(v, 10)).search(option.data(v));
            });
            table.draw();
        }
    });
});

With this HTML:

<select name="filter">
    <option value="">No filter</option>
    <option data-1="Accountant">Filter Accountant</option>
    <option data-2="Tokyo">Filter Tokyo</option>
    <option data-1="Accountant" data-2="Tokyo">Filter Accountant in Tokyo</option>
</select>

Should do what you need. We iterate over the data attributes of the selected option and search the relevant column. Working JSFiddle here.

Hope that helps.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much @annoyingmouse, works like a charme - exactly what I was looking for.
I got an additive question @annoyingmouse. Is it also possible to filter for hidden columns, like calling the column by name or somethink like that?
That's very cool, thank you. So it's like I have a list of all my columns (like my database), independent from my shown table - and the indexes alway remain the same, no matter which columns are hidden or shown? @annoyingmouse
That's sort of right. It depends if they're in your data. I guess it'll depend upon how the data is generated and placed within the Table or DOM.
One month later and another question rised. How could I search for empty columns? A simple <option data-8="">Some text</option> doesn't seem to work. @annoyingmouse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.