Multi Column Filtering
Multi Column Filtering
It seems like your fnFilter function allows for only filtering on a single columns data.
Yet for some reason when i call it, i see the other columns filter options being passed. That doesn't appear to be a part of your spec.
How is it supposed to work?
If i want to filter on multiple columns at once(I am using server side processing) how do i do this?
Thanks so much for making a great product!
Yet for some reason when i call it, i see the other columns filter options being passed. That doesn't appear to be a part of your spec.
How is it supposed to work?
If i want to filter on multiple columns at once(I am using server side processing) how do i do this?
Thanks so much for making a great product!
This discussion has been closed.
Replies
Regards,
Allan
for ex:
if i do fnFilter("Games", 1) it sets the sSearch_1="Games" and sends a request to my server.
If i then do fnFilter("Action", 2) it sets sSearch_2 ="Action" but the request that is sent to the server sends both sSearch_1 and sSearch_2 with their values, so then i can filter on both column on the server side.
No that, doesn't seem to match your spec, why would it send both columns filters.
The fact that it does though is nice for me, because i want to be able to filter on multiple columns at once.
The problem, is that the only way that i have found to do this, is to call the fnFilter multiple times, which is hardly optimal. Because for each row, i want to have a filter on, i have cal the function once.
rather then say fnFilter({sSearch_1:"Games", sSearch_2:"Action",...}.
Which in reality, i think would make way more sense if whenever you were doing filtering(at least serverside filtering), you would be more likely to want to filter on column names. fnFilter({ItemType:"Game", Category:"Action"}. This way you don't have to care about indexes.
Can i do what i want to do another way?
The fnFilter function, as you note, will "build up" a multiple column search, giving the ability to set only one of the search parameters (global, or one of the columns) at a time. This isn't exactly ideal from the point of view of server-side processing, due to the multiple requests.
So what you can do (there isn't an API function for this yet, but it would probably be fairly easy to write one) is to make use of the DataTables settings object (fnSettings()) and manipulate the internal parameters. If you take a peak at the code for fnFilter you will see:
[code]
/* Single column filter */
oSettings.aoPreSearchCols[ iColumn ].sSearch = sInput;
oSettings.aoPreSearchCols[ iColumn ].bEscapeRegex = bEscapeRegex;
_fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );
[/code]
You basically want to do the same thing. Set aoPreSearchCols for the columns you want to search on and then call fnDraw().
To add in the option of using names, again is quite possible, using the sName parameter for the columns. It would just need a look up loop to find the index based on the name, and then set aoPreSearchCols as needed.
Regards,
Allan
I wrote the functions below, feel free to use/modify them.
One last question, I want to be able have this filter information be used on table init..(ie the first time the table loads its data).
So ideally, i would set configuration parameter of [code]
oDefaultFilters :{ "sNameOfColumn":"filterValue"}.[/code]
Where is the correct place in your code to add a check to see if the sDefaultFilter paramemter is set, then
perhaps call my _fnSetMultiFilterData function, or something similiar?
The Code is below.
[code]
Added this line: this.oApi._fnSetMultiFilterData = _fnSetMultiFilterData; to api object
/*
* Function: _fnSetMultiFilterData
Purpose: generates oSettings ojbect w/ sSearch parameters set
Returns: oSettings Object
Inputs:
oSettings Object: - a json object contains the table settings. retrieved from a call to var oSettings = _fnSettingsFromNode( this[_oExt.iApiIndex] );
jsonObjec: - a json object that contains the sName Parameter of the column to be filter, as well as the value to be filtered on.
{columnName:filterValue}
*/
_fnSetMultiFilterData = function(oSettings, fData){
//Loop through all elements in fData and configure them in the settings.
for (var key in fData) {
if (fData.hasOwnProperty(key)) {
//Loop through all the columns and configure the column w/ the matching name
for(var i in oSettings.aoColumns){
// console.log(fData);
// console.log(key);
// console.log(oSettings.aoColumns);
if(oSettings.aoColumns[i].sName==key){
/* Single column filter */
oSettings.aoPreSearchCols[ i ].sSearch = fData[key];
}
}
}
}
return oSettings;
}
/*
* Function: fnMultiFilter
Purpose: filterTable on multiple columns at once
Returns: -
Inputs:
jsonObjec: - a json object that contains the sName Parameter of the column to be filter, as well as the value to be filtered on.
{columnName:filterValue}
*/
this.fnMultiFilter = function(fData){
var oSettings = _fnSettingsFromNode( this[_oExt.iApiIndex] );
var oSettings = _fnSetMultiFilterData(oSettings,fData);
//Loop through all elements in fData and configure them in the settings.
_fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );
}
[/code]
This allows me to do:
[code]
var defaultFilters = {"listingOutlet":"zappos","zj":"1"};
oTable.fnMultiFilter(defaultFilters);
[/code]
I've packaged up your addition into an API plug-in: http://datatables.net/plug-ins/api#fnMultiFilter . If you would like the credit altered from just your forum screen name - just give me a shout :-)
Regarding the ability to do this on initialisation - the place to do it is in the "Constructor" - search for "Section - Constructor" in the code, and you will see a lot of calls to _fnMap, just under there would be the best place. however, it is actually to some degree already possible by using aoSearchCols ( http://datatables.net/usage/options#aoSearchCols ) - the only thing missing would be the name - but you might already now the ordering at this point.
Regards,
Allan
- Filter.js:
[code]
$.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
any suggestions about this issue?
thanks!!!
Because I didn't need to use sName, I re-wrote your function:
[code]$.fn.dataTableExt.oApi.fnArrayFilter = function(oSettings, aData) {
for (var i = 0; i < aData.length; i++) {
oSettings.aoPreSearchCols[i].sSearch = aData[i];
}
this.oApi._fnDraw( oSettings );
}
[/code]
How can we get rid of that keyup event handler ???
Why does "oSettings.aoPreSearchCols[ i ].sSearch = value" binds a a keyup event to the input ?