Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82115

Multi column filtering and displaying rows where the filter term exists in either of 2 columns

$
0
0
Page http://datatables.net/plug-ins/api#fnMultiFilter contains an example for using fnMultiFilter:

$.fn.dataTableExt.oApi.fnMultiFilter = function( oSettings, oData ) {
for ( var key in oData )
{
if ( oData.hasOwnProperty(key) )
{
for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
{
if( oSettings.aoColumns[i].sName == key )
{
/* Add single column filter */
oSettings.aoPreSearchCols[ i ].sSearch = oData[key];
break;
}
}
}
}
this.oApi._fnDraw( oSettings );
};


$(document).ready(function() {
var oTable = $('#example').dataTable( {
"aoColumns": [
{ "sName": "engine" },
{ "sName": "browser" },
{ "sName": "platform" },
{ "sName": "version" },
{ "sName": "grade" }
]
} );
oTable.fnMultiFilter( { "engine": "Gecko", "browser": "Cam" } );
} );


If I understand it correctly, this example will ONLY display rows that have 'Gecko' in the 'engine' column, AND 'Cam' in the 'browser' column. My needs are a little different. I want the user to enter a filter term and be able to display rows that have that string in EITHER of those 2 columns. For example, say my table looks like this:

engine browser column3
------ ------- -------
netscape firefox something
gecko firefox else
gecko mozilla dont
gecko netscape care

If the user types in netscape, I want rows 1 and 4 to be displayed. However, using the plug-in above, if I specified this:

oTable.fnMultiFilter( { "engine": "netscape", "browser": "netscape" } );

then no rows will be displayed, because there aren't any rows that have netscape as BOTH an engine and a browser.

Another way of saying this is the following: by default, the filtering mechanism will do a global filter, and display all rows where the filtering term exists in ANY of the columns. I want to modify that a little, so that it displays all rows where the filtering term exists in EITHER of the first 2 columns, even though my table has 8 columns. I don't care if the term exists in the other 6 columns, I'm only interested in the first 2.

Thanks for the help!

Viewing all articles
Browse latest Browse all 82115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>