3

I am using the jQuery DataTables plugin to do filtering on a set of table results. My table lists customers and members which they are a group of. I have a dropdown that allows the user to filter the table for those customers who members of a particular group. My problems is that customers can be members of multiple groups and all of these are listed in a single column.

For example, Joe might be a member of the following groups:

  • Group 1
  • Group 5
  • Group 10

If I do regular filtering (see below), and the user selects "Group 1" from the dropdown, it will still show customers who are members of "Group 10".

function fnFilterColumn ( i ){
    $('#results').dataTable().fnFilter(
        $("#filter").val(),
        i,
        false            
    );
}

If I enable Regex (see below), then it does an "exact" match. So if the user selects "Group 1" from the dropdown, it will only show customers who are only members of "Group 1".

function fnFilterColumn ( i ){
     $('#results').dataTable().fnFilter(
         '^' + $("#filter").val() + '$',
         i,
         true        
    );
}

How would I go about making it filter for "whole phrase matching". So a filter for "Group 1" will show those in "Group 1", without grabbing "Group 10" too. Any ideas?

1 Answer 1

2

The regex approach seems to be sensible. Just use word boundaries instead of start / end anchors:

function fnFilterColumn ( i ){
    $('#results').dataTable().fnFilter(
        '\\b' + $("#l"+(i)+"_filter").val() + '\\b',
        i,
        true        
    );
}

If you do it this way, you will match multiple groups too.

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

Comments

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.