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?
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?
Share Improve this question asked Mar 27, 2013 at 15:55 JoeJoe 82.7k18 gold badges129 silver badges147 bronze badges 3-
The doc says you can pass null as the second parameter to filter all columns, I haven't tried it though.
dataTable.fnFilter("[^0]", null, true);
Won't work if you need to be specific about which columns are ORed. – James Commented Mar 27, 2013 at 16:00 - Yeah, I was hoping I could find a way to send an array of indeces instead of a single index or global. – Joe Commented Mar 27, 2013 at 16:09
- You can roll your own filtering functions too: link – James Commented Mar 27, 2013 at 16:14
2 Answers
Reset to default 4Using 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
Just in case you might need that functionality wrapped up into some user-interface, you may go and check out this DataTables plug-in, in addition to conventional 'AND'-logic you may apply 'OR'-filtering.