FnFilter OR gate style search multiple columns solution
FnFilter OR gate style search multiple columns solution
I started using Datatables about a week ago, so in no shape or form do I claim to be Allan Jardine Jr.
My objective is to search specify column(s) simultaneously. Here is what I did (after spending 3 long days looking at the datatable code. sigh):
[code]
/*
isAll - search all columns in colArray.
colArray - Array of all columns in tables. This isn't necessary but to make
it generic enough for everyone else. For example, my first column is a
checkbox, obviously we wouldn't want to search this.
colsToSearch - Array of specific columns in colArray to search on.
*/
function searchDTColumns(isAll, colArray, colsToSearch)
{
/*get settings for the datatable*/
var oSettings = oTable.fnSettings();
/*search all columns*/
if(isAll)
{
for(var i = 0; i < colArray.length; i++)
{
oSettings.aoColumns[colArray[i]].bSearchable = true;
}
}
else /*search specify columns*/
{
/*reset all columns searchable to false*/
for(var j = 0; j < colArray.length; j++)
{
oSettings.aoColumns[colArray[j]].bSearchable = false;
}
/*set the specify columns searchable to true*/
for(var k = 0; k < colsToSearch.length; k++)
{
oSettings.aoColumns[colsToSearch[k]].bSearchable = true;
}
}
}
[/code]
Again, I didn't say it was a sophisticated custom filter but it gets the job done. Here is how I call it:
[code]
/*event handler for search input field*/
$('#btSearch').keyup( function(e) {
/*All possible columns to search*/
var allCols = [1,2,3,4,5,6,7,8,9,10,11,12];
/*array of selected columns to search. I use a select tag with the chosen plug-in.*/
var sel = $('#btSrchOptn').val();
/*user wants to search all columns*/
if((sel == 'All') || (sel == null))
{
searchDTColumns(true, allCols);
}
/*user wants to search specific column(s)*/
else if((sel != 'All') || ($.inArray("All", sel) == -1))
{
searchDTColumns(false, allCols, sel);
}
oTable.fnFilter( this.value);
} );
[/code]
Hopefully this will help someone else out. I'm also looking for someone to point out any mistakes or anywhere I can optimize. Hopefully if can be a plug-in one day.
Cheers!
My objective is to search specify column(s) simultaneously. Here is what I did (after spending 3 long days looking at the datatable code. sigh):
[code]
/*
isAll - search all columns in colArray.
colArray - Array of all columns in tables. This isn't necessary but to make
it generic enough for everyone else. For example, my first column is a
checkbox, obviously we wouldn't want to search this.
colsToSearch - Array of specific columns in colArray to search on.
*/
function searchDTColumns(isAll, colArray, colsToSearch)
{
/*get settings for the datatable*/
var oSettings = oTable.fnSettings();
/*search all columns*/
if(isAll)
{
for(var i = 0; i < colArray.length; i++)
{
oSettings.aoColumns[colArray[i]].bSearchable = true;
}
}
else /*search specify columns*/
{
/*reset all columns searchable to false*/
for(var j = 0; j < colArray.length; j++)
{
oSettings.aoColumns[colArray[j]].bSearchable = false;
}
/*set the specify columns searchable to true*/
for(var k = 0; k < colsToSearch.length; k++)
{
oSettings.aoColumns[colsToSearch[k]].bSearchable = true;
}
}
}
[/code]
Again, I didn't say it was a sophisticated custom filter but it gets the job done. Here is how I call it:
[code]
/*event handler for search input field*/
$('#btSearch').keyup( function(e) {
/*All possible columns to search*/
var allCols = [1,2,3,4,5,6,7,8,9,10,11,12];
/*array of selected columns to search. I use a select tag with the chosen plug-in.*/
var sel = $('#btSrchOptn').val();
/*user wants to search all columns*/
if((sel == 'All') || (sel == null))
{
searchDTColumns(true, allCols);
}
/*user wants to search specific column(s)*/
else if((sel != 'All') || ($.inArray("All", sel) == -1))
{
searchDTColumns(false, allCols, sel);
}
oTable.fnFilter( this.value);
} );
[/code]
Hopefully this will help someone else out. I'm also looking for someone to point out any mistakes or anywhere I can optimize. Hopefully if can be a plug-in one day.
Cheers!
This discussion has been closed.