Example Fiddle
I have an html table:
_A_B_C_D_
|0|1|0|1|
|0|1|0|0|
|1|0|0|1|
I want to filter non-zero columns. Using jQuery dataTables (not a hard requirement, just what I am currently using) I execute the following filter:
// value is the column index, true simply informs the filter method to use regex
dataTable.fnFilter("[^0]", value, true);
However, filtering multiple columns creates an AND filter. Thus:
dataTable.fnFilter("[^0]", 0 /*A*/, true);
dataTable.fnFilter("[^0]", 3 /*D*/, true);
would create the following
_A_B_C_D_
|1|0|0|1|
I need OR behavior though which would create the following table:
_A_B_C_D_
|0|1|0|1|
|1|0|0|1|
Where Column A is non-zero OR Column D is non-zero. I cannot think of any way to implement this with my current structure.
How can I filter table columns using OR logic as apposed to AND?
Using your fiddle as a model I updated it to provide the functionality that I believe you want.
I extend the dataTables filtering on each click of a checkbox:
$.fn.dataTableExt.afnFiltering.push(
function (settings, data, index) {
var s = [0, 3, 5]; // columns to filter
for (var i=0; i < s.length; i++) {
if (data[s[i]] != 0) return true;
}
return false;
}
);
Fully integrated into fiddle: link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With