Filtering Table with Checkbox Values
Filtering Table with Checkbox Values
studio6000
Posts: 7Questions: 0Answers: 0
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?
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?
This discussion has been closed.
Replies
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.