Patch for fast exact filtering on raw data

Patch for fast exact filtering on raw data

MitchCapperMitchCapper Posts: 3Questions: 0Answers: 0
edited September 2012 in General
DataTables amazes me with its ability to handle large data sets with ease (bDeferRender/mRender allow me to do thousands of rows in under a second) and its filtering functionality is great. I ran into a situation where I needed to filter the large amount of data in a table frequently but did not need the fuzzy matching or output value filtering DataTables normally does. What I needed was to filter based on the raw table data and match an exact value. Originally I looked at trying to write this like the API but then I ended up with duplicating a lot of existing function code (which would not survive through new releases) so I figured I would just post it as a patch so the original author or others could do with it what they like.

In my case I was going to always be filtering the table based on this exact match system so right now the code uses a option "bExactFiltering" to turn on the exact filtering. If you want to only sometimes do this (and if it was integrated into the main DT code obviously probably the way to go) adding a parameter to fnFilter for exact / raw filtering may be advantageous. The advantage of this code verses using the regexp that seems to be the suggest way otherwise should be:
1) Exact comparisons should be somewhat faster than regexp checking
2) Normal filtering has a lot of call outs to get the data to filter verse this code which only matches on the raw table data

To use this code add the following functions to the jQuery.datatable.js class before:
[code]function _fnAddColumn( oSettings, nTh )[/code]
add:
[code]
function _fnFilterExact( oSettings, sInput, iForce, bRegex, bSmart, bCaseInsensitive ) {
if ( sInput.length <= 0 )//if blank just call the normal one
_fnFilter( oSettings, sInput, iForce, bRegex, bSmart, bCaseInsensitive )
else
_fnBuildSearchArrayExact(oSettings, 1, sInput);
}
function _fnBuildSearchArrayExact( oSettings, iMaster, SearchID ) {
oSettings.asDataSearch = [];
var aiFilterColumns = _fnGetColumns( oSettings, 'bSearchable' );
var aiIndex = (iMaster===1) ? oSettings.aiDisplayMaster : oSettings.aiDisplay;
oSettings.aiDisplay.splice( 0, oSettings.aiDisplay.length);
var col_indexes = [];
for ( var j=0, jLen=aiFilterColumns.length ; j

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Hi,

    This is fantastic - thanks very much for posting this! My plan is, in 1.11 ( http://datatables.net/development/roadmap ) to complete the modularisation of DataTables and allow the complete replacement of the built in modules, such as filtering, to increase the fuzzy matching options, or to optimise for speed such as here. So I think this will prove to be extremely useful when we get to that point (might be able to wrap it up into the first module :-) ).

    Regards,
    Allan
This discussion has been closed.