Filtering Table with Checkbox Values

Filtering Table with Checkbox Values

studio6000studio6000 Posts: 7Questions: 0Answers: 0
edited November 2010 in General
Hi,

Awesome script!

I need to filter my table based on multiple selected options. So I am using checkboxes instead of a select drop down box. The problem I am having, is that it will only filter one option at a time. This is the code I came up with.

[code]
$("#location_select_box").each( function () {
this.innerHTML = fnCreateCheckbox( oTable.fnGetColumnData(5) );
} );

$(\'#location_checkbox\', this).change( function () {
oTable.fnFilter( $(this).val(), 5 );
} );

[/code]

Any idea what I'm doing wrong?

Replies

  • LittleTreeXLittleTreeX Posts: 3Questions: 0Answers: 0
    edited December 2010
    This may be late, but I thought this may be helpful for others (lord knows I searched a lot for this answer).

    I found the easiest solution for this was to leverage the power of regular expressions in your filtering. This may or may not be the most elegant / efficient way, but it seems to work rather well:
    HTML:
    [code]
    Filter 1

    Filter 2

    Filter 3

    Filter 4

    [/code]

    NOTE: filter_str_1 - filter_str_2 are the strings you'd like to filter.

    The JavaScript:
    [code]
    $('input[name="filter"]').click(function() {
    var reg_exp = '';
    var checkboxs = document.getElementsByName('filter');
    for(var i = 0, inp; inp = checkboxs[i]; i++) {
    if (inp.type.toLowerCase() == 'checkbox' && inp.checked) {
    reg_exp = reg_exp + inp.value + '|';
    //the | symbol is like an OR
    }
    }
    //passing an empty string will result in no filter
    //thus, it must be set to something that will not exist in the column
    if (reg_exp == '') {reg_exp = 'X|' }

    oTable.fnFilter( reg_exp.slice(0, -1),5, true );
    //slice off the last '|' or it doesn't work
    //also be sure to use the third parameter
    });
    [/code]

    Instead of using a loop on every click to recreate the regular expression string, you could potentially use some sort of add / replace string function, and simply keep a persistent copy of the filter string. I'm not too sure which would be better.
    I hope this helps someone.
  • LittleTreeXLittleTreeX Posts: 3Questions: 0Answers: 0
    edited December 2010
    Removed.
This discussion has been closed.