Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 605
Joe Avatar asked Oct 05 '22 14:10

Joe


1 Answers

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

like image 151
James Avatar answered Oct 10 '22 03:10

James