Help filtering multiple strings

Help filtering multiple strings

nsargentnsargent Posts: 1Questions: 0Answers: 0
edited October 2013 in DataTables 1.8
Hello,

I have a page with two checkboxes, #Blue and #Red, that I'd like to use for filtering the sample table below. A user should be able to check one, both, or neither checkbox. The checkboxes should filter the table based on keywords (colorBlue and colorRed) which are contained in a hidden column.

Here's what 4 table rows might look like (with the 2nd column being hidden but containing the keywords):

[code]

table row 1
 


table row 2
colorBlue


table row 3
colorRed


table row 4
colorBlue colorRed

[/code]

When a user checks (or unchecks) either checkbox, the following function is called:

[code]
function checkFilters () {
if ( $("#Blue").is(":checked") &&
$("#Red").is(":checked")) {
TableList.fnFilter('colorBlue|colorRed', 1, true, false);
}
if ( $("#Blue").is(":not(:checked)") &&
$("#Red").is(":checked")) {
TableList.fnFilter('colorRed', 1, true, false);
}
if ( $("#Blue").is(":checked") &&
$("#Red").is(":not(:checked)")) {
TableList.fnFilter('colorBlue', 1, true, false);
}
if ( $("#Blue").is(":not(:checked)") &&
$("#Red").is(":not(:checked)")) {
TableList.fnFilter('', 1, true, false);
}
};
[/code]

I've determined that the function is indeed being called each time a checkbox is checked or unchecked but the if statements do not seem to be evaluated or run (or at least the table isn't being filtered). If a user were to type "Blue Red" in the search box then it would filter and only show table row 4. Basically I'm trying to achieve that same result if the user were to check both the Blue and Red checkboxes.

My javascript skills are certainly 'beginner' so I realize the code about may not be ideal. My question really however, is really whether the following should in fact work for filtering multiple strings:

[code]
TableList.fnFilter('colorBlue|colorRed', 1, true, false);
[/code]

Thanks so much!

Replies

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin
    > but the if statements do not seem to be evaluated or run

    We are going to need a link to a test case I'm afraid please. It looks like your code should actually work as you expect, so I'm not sure what's going wrong immediately.

    Allan
This discussion has been closed.