FnFilter Conflict

FnFilter Conflict

bryceray1121bryceray1121 Posts: 65Questions: 0Answers: 0
edited February 2011 in General
I use a custom date sort filter for my tables. However, I've run into a problem that appears to be caused by a collision between my custom filter and the default filter.

Situation:
I initialize a table with this filter code:
[code]
var defaultMin = new Date(new Date().setMonth(new Date().getMonth() - 6)).getTime();
var filterDateTables = {};
filterDateTables["#labs-table"] = defaultMin;
filterDateTables["#labResults-table"] = defaultMin;

$jq.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
var iMin = null;
var tableSrc = null;
for(key in filterDateTables){ //Check if the active table is in the the filterDateTables array
tableSrc = $jq(key)[0];
if(tableSrc == oSettings.nTable){
iMin = filterDateTables[key];
break;
}
}
if ( iMin == null )
{
return true;
}

var iMax = new Date().getTime();
var colIndex = $jq(".filterDate",tableSrc).parent().children().index($jq(".filterDate",tableSrc)); //Dynamically determine the index of the column which stores the date type
var iVersion = aData[colIndex] == "-" ? 0 : new Date(aData[colIndex]).getTime();
if ( iMin == "" && iMax == "" ) {
return true;
}
else if ( iMin == "" && iVersion < iMax ) {
return true;
}
else if ( iMin < iVersion && "" == iMax ) {
return true;
}
else if ( iMin < iVersion && iVersion < iMax ) {
return true;
}
return false;

}
);

filterByDate = function(id,months){ //onclick function
var min = "";
if($jq(id).is(':visible')){
if(months !== undefined){ //If no month is defined display all results
min = new Date(new Date().setMonth(new Date().getMonth() - months)).getTime(); //Subtract user defined months from current date to determine minimum date.
}
filterDateTables[id] = min; //Set min
$jq(id).dataTable().fnDraw(); //Apply filter
}
}
[/code]

I then want to go to that table and do a regular text search using the default fnfilter. However, when I do this the table automatically returns no results. Even though there are results to be returned. If I then temporarily disable the above code and do the same thing it returns results.

Any ideas on why the above code is breaking the default fnfilter?

Let me know if you need more information.

THanks.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    The way the filtering works in DataTables is that it is cumulative - so for a row to be shown, it must be allowed by both the global filter and your custom filter. So first thing to check - without your custom filter, does the filter work as you would expect? If that's all good (and I sure hope it is!) I'd suggest adding in a console.log() message just before each one of your returns in the custom filter and check that true is being returned for the rows you would expect.

    Allan
This discussion has been closed.