1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-02-27 00:54:15 +01:00

New: Filtering now has a case insenstive option - this is bCaseInsensitive in oSearch, or the 6th parameter for fnFilter

This commit is contained in:
Allan Jardine 2011-12-14 15:26:37 +00:00
parent 7189aec651
commit 6db08eb107
6 changed files with 114 additions and 60 deletions

View File

@ -90,15 +90,22 @@
} }
else else
{ {
/* Don't require that the user must specify bRegex and / or bSmart */ var oPre = oSettings.aoPreSearchCols[ iCol ];
if ( oSettings.aoPreSearchCols[ iCol ].bRegex === undefined )
/* Don't require that the user must specify bRegex, bSmart or bCaseInsensitive */
if ( oPre.bRegex === undefined )
{ {
oSettings.aoPreSearchCols[ iCol ].bRegex = true; oPre.bRegex = true;
} }
if ( oSettings.aoPreSearchCols[ iCol ].bSmart === undefined ) if ( oPre.bSmart === undefined )
{ {
oSettings.aoPreSearchCols[ iCol ].bSmart = true; oPre.bSmart = true;
}
if ( oPre.bCaseInsensitive === undefined )
{
oPre.bCaseInsensitive = true;
} }
} }
@ -2025,13 +2032,14 @@
function _fnFilterComplete ( oSettings, oInput, iForce ) function _fnFilterComplete ( oSettings, oInput, iForce )
{ {
/* Filter on everything */ /* Filter on everything */
_fnFilter( oSettings, oInput.sSearch, iForce, oInput.bRegex, oInput.bSmart ); _fnFilter( oSettings, oInput.sSearch, iForce, oInput.bRegex, oInput.bSmart, oInput.bCaseInsensitive );
/* Now do the individual column filter */ /* Now do the individual column filter */
for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ ) for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ )
{ {
_fnFilterColumn( oSettings, oSettings.aoPreSearchCols[i].sSearch, i, _fnFilterColumn( oSettings, oSettings.aoPreSearchCols[i].sSearch, i,
oSettings.aoPreSearchCols[i].bRegex, oSettings.aoPreSearchCols[i].bSmart ); oSettings.aoPreSearchCols[i].bRegex, oSettings.aoPreSearchCols[i].bSmart,
oSettings.aoPreSearchCols[i].bCaseInsensitive );
} }
/* Custom filtering */ /* Custom filtering */
@ -2087,9 +2095,10 @@
* @param {int} iColumn column to filter * @param {int} iColumn column to filter
* @param {bool} bRegex treat search string as a regular expression or not * @param {bool} bRegex treat search string as a regular expression or not
* @param {bool} bSmart use smart filtering or not * @param {bool} bSmart use smart filtering or not
* @param {bool} bCaseInsensitive Do case insenstive matching or not
* @private * @private
*/ */
function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart ) function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart, bCaseInsensitive )
{ {
if ( sInput === "" ) if ( sInput === "" )
{ {
@ -2097,7 +2106,7 @@
} }
var iIndexCorrector = 0; var iIndexCorrector = 0;
var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart ); var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart, bCaseInsensitive );
for ( var i=oSettings.aiDisplay.length-1 ; i>=0 ; i-- ) for ( var i=oSettings.aiDisplay.length-1 ; i>=0 ; i-- )
{ {
@ -2119,12 +2128,13 @@
* @param {int} iForce optional - force a research of the master array (1) or not (undefined or 0) * @param {int} iForce optional - force a research of the master array (1) or not (undefined or 0)
* @param {bool} bRegex treat as a regular expression or not * @param {bool} bRegex treat as a regular expression or not
* @param {bool} bSmart perform smart filtering or not * @param {bool} bSmart perform smart filtering or not
* @param {bool} bCaseInsensitive Do case insenstive matching or not
* @private * @private
*/ */
function _fnFilter( oSettings, sInput, iForce, bRegex, bSmart ) function _fnFilter( oSettings, sInput, iForce, bRegex, bSmart, bCaseInsensitive )
{ {
var i; var i;
var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart ); var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart, bCaseInsensitive );
/* Check if we are forcing or not - optional parameter */ /* Check if we are forcing or not - optional parameter */
if ( !iForce ) if ( !iForce )
@ -2195,6 +2205,7 @@
oSettings.oPreviousSearch.sSearch = sInput; oSettings.oPreviousSearch.sSearch = sInput;
oSettings.oPreviousSearch.bRegex = bRegex; oSettings.oPreviousSearch.bRegex = bRegex;
oSettings.oPreviousSearch.bSmart = bSmart; oSettings.oPreviousSearch.bSmart = bSmart;
oSettings.oPreviousSearch.bCaseInsensitive = bCaseInsensitive;
} }
@ -2265,10 +2276,11 @@
* @param {string} sSearch string to search for * @param {string} sSearch string to search for
* @param {bool} bRegex treat as a regular expression or not * @param {bool} bRegex treat as a regular expression or not
* @param {bool} bSmart perform smart filtering or not * @param {bool} bSmart perform smart filtering or not
* @param {bool} bCaseInsensitive Do case insenstive matching or not
* @returns {RegExp} constructed object * @returns {RegExp} constructed object
* @private * @private
*/ */
function _fnFilterCreateSearch( sSearch, bRegex, bSmart ) function _fnFilterCreateSearch( sSearch, bRegex, bSmart, bCaseInsensitive )
{ {
var asSearch, sRegExpString; var asSearch, sRegExpString;
@ -2279,12 +2291,12 @@
*/ */
asSearch = bRegex ? sSearch.split( ' ' ) : _fnEscapeRegex( sSearch ).split( ' ' ); asSearch = bRegex ? sSearch.split( ' ' ) : _fnEscapeRegex( sSearch ).split( ' ' );
sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$'; sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$';
return new RegExp( sRegExpString, "i" ); return new RegExp( sRegExpString, bCaseInsensitive ? "i" : "" );
} }
else else
{ {
sSearch = bRegex ? sSearch : _fnEscapeRegex( sSearch ); sSearch = bRegex ? sSearch : _fnEscapeRegex( sSearch );
return new RegExp( sSearch, "i" ); return new RegExp( sSearch, bCaseInsensitive ? "i" : "" );
} }
} }
@ -5192,11 +5204,12 @@
/** /**
* Filter the input based on data * Filter the input based on data
* @param {string} sInput string to filter the table on * @param {string} sInput String to filter the table on
* @param {int|null} [iColumn] column to limit filtering to * @param {int|null} [iColumn] Column to limit filtering to
* @param {bool} [bRegex=false] treat as regular expression or not * @param {bool} [bRegex=false] Treat as regular expression or not
* @param {bool} [bSmart=true] perform smart filtering or not * @param {bool} [bSmart=true] Perform smart filtering or not
* @param {bool} [bShowGlobal=true] show the input global filter in it's input box(es) * @param {bool} [bShowGlobal=true] Show the input global filter in it's input box(es)
* @param {bool} [bCaseInsensitive=true] Do case-insensitive matching (true) or not (false)
* *
* @example * @example
* $(document).ready(function() { * $(document).ready(function() {
@ -5206,7 +5219,7 @@
* oTable.fnFilter( 'test string' ); * oTable.fnFilter( 'test string' );
* } ); * } );
*/ */
this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal ) this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal, bCaseInsensitive )
{ {
var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] ); var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
@ -5215,28 +5228,34 @@
return; return;
} }
if ( bRegex === undefined ) if ( bRegex === undefined || bRegex === null )
{ {
bRegex = false; bRegex = false;
} }
if ( bSmart === undefined ) if ( bSmart === undefined || bSmart === null )
{ {
bSmart = true; bSmart = true;
} }
if ( bShowGlobal === undefined ) if ( bShowGlobal === undefined || bShowGlobal === null )
{ {
bShowGlobal = true; bShowGlobal = true;
} }
if ( bCaseInsensitive === undefined || bCaseInsensitive === null )
{
bCaseInsensitive = true;
}
if ( !iColumn ) if ( !iColumn )
{ {
/* Global filter */ /* Global filter */
_fnFilterComplete( oSettings, { _fnFilterComplete( oSettings, {
"sSearch":sInput+"", "sSearch":sInput+"",
"bRegex": bRegex, "bRegex": bRegex,
"bSmart": bSmart "bSmart": bSmart,
"bCaseInsensitive": bCaseInsensitive
}, 1 ); }, 1 );
if ( bShowGlobal && oSettings.aanFeatures.f ) if ( bShowGlobal && oSettings.aanFeatures.f )
@ -5254,7 +5273,8 @@
$.extend( oSettings.aoPreSearchCols[ iColumn ], { $.extend( oSettings.aoPreSearchCols[ iColumn ], {
"sSearch": sInput+"", "sSearch": sInput+"",
"bRegex": bRegex, "bRegex": bRegex,
"bSmart": bSmart "bSmart": bSmart,
"bCaseInsensitive": bCaseInsensitive
} ); } );
_fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 ); _fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );
} }
@ -6969,6 +6989,13 @@
* @namespace * @namespace
*/ */
DataTable.models.oSearch = { DataTable.models.oSearch = {
/**
* Flag to indicate if the filtering should be case insensitive or not
* @type boolean
* @default true
*/
"bCaseInsensitive": true,
/** /**
* Applied search term * Applied search term
* @type string * @type string
@ -8815,11 +8842,11 @@
/** /**
* This parameter allows you to have define the global filtering state at * This parameter allows you to have define the global filtering state at
* initialisation time. As an object the "sSearch" parameter must be * initialisation time. As an object the "sSearch" parameter must be
* defined, but the "bRegex" and "bSmart" parameters are optional. When * defined, but all other parameters are optional. When "bRegex" is true,
* "bRegex" is true, the search string will be treated as a regular * the search string will be treated as a regular expression, when false
* expression, when false (default) it will be treated as a straight string. * (default) it will be treated as a straight string. When "bSmart"
* When "bSmart" DataTables will use it's smart filtering methods (to word * DataTables will use it's smart filtering methods (to word match at
* match at any point in the data), when false this will not be done. * any point in the data), when false this will not be done.
* @type object * @type object
* @extends DataTable.models.oSearch * @extends DataTable.models.oSearch
* *

View File

@ -509,11 +509,12 @@ this.fnDraw = function( bComplete )
/** /**
* Filter the input based on data * Filter the input based on data
* @param {string} sInput string to filter the table on * @param {string} sInput String to filter the table on
* @param {int|null} [iColumn] column to limit filtering to * @param {int|null} [iColumn] Column to limit filtering to
* @param {bool} [bRegex=false] treat as regular expression or not * @param {bool} [bRegex=false] Treat as regular expression or not
* @param {bool} [bSmart=true] perform smart filtering or not * @param {bool} [bSmart=true] Perform smart filtering or not
* @param {bool} [bShowGlobal=true] show the input global filter in it's input box(es) * @param {bool} [bShowGlobal=true] Show the input global filter in it's input box(es)
* @param {bool} [bCaseInsensitive=true] Do case-insensitive matching (true) or not (false)
* *
* @example * @example
* $(document).ready(function() { * $(document).ready(function() {
@ -523,7 +524,7 @@ this.fnDraw = function( bComplete )
* oTable.fnFilter( 'test string' ); * oTable.fnFilter( 'test string' );
* } ); * } );
*/ */
this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal ) this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal, bCaseInsensitive )
{ {
var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] ); var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
@ -532,28 +533,34 @@ this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal )
return; return;
} }
if ( bRegex === undefined ) if ( bRegex === undefined || bRegex === null )
{ {
bRegex = false; bRegex = false;
} }
if ( bSmart === undefined ) if ( bSmart === undefined || bSmart === null )
{ {
bSmart = true; bSmart = true;
} }
if ( bShowGlobal === undefined ) if ( bShowGlobal === undefined || bShowGlobal === null )
{ {
bShowGlobal = true; bShowGlobal = true;
} }
if ( bCaseInsensitive === undefined || bCaseInsensitive === null )
{
bCaseInsensitive = true;
}
if ( !iColumn ) if ( !iColumn )
{ {
/* Global filter */ /* Global filter */
_fnFilterComplete( oSettings, { _fnFilterComplete( oSettings, {
"sSearch":sInput+"", "sSearch":sInput+"",
"bRegex": bRegex, "bRegex": bRegex,
"bSmart": bSmart "bSmart": bSmart,
"bCaseInsensitive": bCaseInsensitive
}, 1 ); }, 1 );
if ( bShowGlobal && oSettings.aanFeatures.f ) if ( bShowGlobal && oSettings.aanFeatures.f )
@ -571,7 +578,8 @@ this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal )
$.extend( oSettings.aoPreSearchCols[ iColumn ], { $.extend( oSettings.aoPreSearchCols[ iColumn ], {
"sSearch": sInput+"", "sSearch": sInput+"",
"bRegex": bRegex, "bRegex": bRegex,
"bSmart": bSmart "bSmart": bSmart,
"bCaseInsensitive": bCaseInsensitive
} ); } );
_fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 ); _fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );
} }

View File

@ -26,15 +26,22 @@ function _fnAddColumn( oSettings, nTh )
} }
else else
{ {
/* Don't require that the user must specify bRegex and / or bSmart */ var oPre = oSettings.aoPreSearchCols[ iCol ];
if ( oSettings.aoPreSearchCols[ iCol ].bRegex === undefined )
/* Don't require that the user must specify bRegex, bSmart or bCaseInsensitive */
if ( oPre.bRegex === undefined )
{ {
oSettings.aoPreSearchCols[ iCol ].bRegex = true; oPre.bRegex = true;
} }
if ( oSettings.aoPreSearchCols[ iCol ].bSmart === undefined ) if ( oPre.bSmart === undefined )
{ {
oSettings.aoPreSearchCols[ iCol ].bSmart = true; oPre.bSmart = true;
}
if ( oPre.bCaseInsensitive === undefined )
{
oPre.bCaseInsensitive = true;
} }
} }

View File

@ -70,13 +70,14 @@ function _fnFeatureHtmlFilter ( oSettings )
function _fnFilterComplete ( oSettings, oInput, iForce ) function _fnFilterComplete ( oSettings, oInput, iForce )
{ {
/* Filter on everything */ /* Filter on everything */
_fnFilter( oSettings, oInput.sSearch, iForce, oInput.bRegex, oInput.bSmart ); _fnFilter( oSettings, oInput.sSearch, iForce, oInput.bRegex, oInput.bSmart, oInput.bCaseInsensitive );
/* Now do the individual column filter */ /* Now do the individual column filter */
for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ ) for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ )
{ {
_fnFilterColumn( oSettings, oSettings.aoPreSearchCols[i].sSearch, i, _fnFilterColumn( oSettings, oSettings.aoPreSearchCols[i].sSearch, i,
oSettings.aoPreSearchCols[i].bRegex, oSettings.aoPreSearchCols[i].bSmart ); oSettings.aoPreSearchCols[i].bRegex, oSettings.aoPreSearchCols[i].bSmart,
oSettings.aoPreSearchCols[i].bCaseInsensitive );
} }
/* Custom filtering */ /* Custom filtering */
@ -132,9 +133,10 @@ function _fnFilterCustom( oSettings )
* @param {int} iColumn column to filter * @param {int} iColumn column to filter
* @param {bool} bRegex treat search string as a regular expression or not * @param {bool} bRegex treat search string as a regular expression or not
* @param {bool} bSmart use smart filtering or not * @param {bool} bSmart use smart filtering or not
* @param {bool} bCaseInsensitive Do case insenstive matching or not
* @private * @private
*/ */
function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart ) function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart, bCaseInsensitive )
{ {
if ( sInput === "" ) if ( sInput === "" )
{ {
@ -142,7 +144,7 @@ function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart )
} }
var iIndexCorrector = 0; var iIndexCorrector = 0;
var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart ); var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart, bCaseInsensitive );
for ( var i=oSettings.aiDisplay.length-1 ; i>=0 ; i-- ) for ( var i=oSettings.aiDisplay.length-1 ; i>=0 ; i-- )
{ {
@ -164,12 +166,13 @@ function _fnFilterColumn ( oSettings, sInput, iColumn, bRegex, bSmart )
* @param {int} iForce optional - force a research of the master array (1) or not (undefined or 0) * @param {int} iForce optional - force a research of the master array (1) or not (undefined or 0)
* @param {bool} bRegex treat as a regular expression or not * @param {bool} bRegex treat as a regular expression or not
* @param {bool} bSmart perform smart filtering or not * @param {bool} bSmart perform smart filtering or not
* @param {bool} bCaseInsensitive Do case insenstive matching or not
* @private * @private
*/ */
function _fnFilter( oSettings, sInput, iForce, bRegex, bSmart ) function _fnFilter( oSettings, sInput, iForce, bRegex, bSmart, bCaseInsensitive )
{ {
var i; var i;
var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart ); var rpSearch = _fnFilterCreateSearch( sInput, bRegex, bSmart, bCaseInsensitive );
/* Check if we are forcing or not - optional parameter */ /* Check if we are forcing or not - optional parameter */
if ( !iForce ) if ( !iForce )
@ -240,6 +243,7 @@ function _fnFilter( oSettings, sInput, iForce, bRegex, bSmart )
oSettings.oPreviousSearch.sSearch = sInput; oSettings.oPreviousSearch.sSearch = sInput;
oSettings.oPreviousSearch.bRegex = bRegex; oSettings.oPreviousSearch.bRegex = bRegex;
oSettings.oPreviousSearch.bSmart = bSmart; oSettings.oPreviousSearch.bSmart = bSmart;
oSettings.oPreviousSearch.bCaseInsensitive = bCaseInsensitive;
} }
@ -310,10 +314,11 @@ function _fnBuildSearchRow( oSettings, aData )
* @param {string} sSearch string to search for * @param {string} sSearch string to search for
* @param {bool} bRegex treat as a regular expression or not * @param {bool} bRegex treat as a regular expression or not
* @param {bool} bSmart perform smart filtering or not * @param {bool} bSmart perform smart filtering or not
* @param {bool} bCaseInsensitive Do case insenstive matching or not
* @returns {RegExp} constructed object * @returns {RegExp} constructed object
* @private * @private
*/ */
function _fnFilterCreateSearch( sSearch, bRegex, bSmart ) function _fnFilterCreateSearch( sSearch, bRegex, bSmart, bCaseInsensitive )
{ {
var asSearch, sRegExpString; var asSearch, sRegExpString;
@ -324,12 +329,12 @@ function _fnFilterCreateSearch( sSearch, bRegex, bSmart )
*/ */
asSearch = bRegex ? sSearch.split( ' ' ) : _fnEscapeRegex( sSearch ).split( ' ' ); asSearch = bRegex ? sSearch.split( ' ' ) : _fnEscapeRegex( sSearch ).split( ' ' );
sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$'; sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$';
return new RegExp( sRegExpString, "i" ); return new RegExp( sRegExpString, bCaseInsensitive ? "i" : "" );
} }
else else
{ {
sSearch = bRegex ? sSearch : _fnEscapeRegex( sSearch ); sSearch = bRegex ? sSearch : _fnEscapeRegex( sSearch );
return new RegExp( sSearch, "i" ); return new RegExp( sSearch, bCaseInsensitive ? "i" : "" );
} }
} }

View File

@ -1499,11 +1499,11 @@ DataTable.models.oInit = {
/** /**
* This parameter allows you to have define the global filtering state at * This parameter allows you to have define the global filtering state at
* initialisation time. As an object the "sSearch" parameter must be * initialisation time. As an object the "sSearch" parameter must be
* defined, but the "bRegex" and "bSmart" parameters are optional. When * defined, but all other parameters are optional. When "bRegex" is true,
* "bRegex" is true, the search string will be treated as a regular * the search string will be treated as a regular expression, when false
* expression, when false (default) it will be treated as a straight string. * (default) it will be treated as a straight string. When "bSmart"
* When "bSmart" DataTables will use it's smart filtering methods (to word * DataTables will use it's smart filtering methods (to word match at
* match at any point in the data), when false this will not be done. * any point in the data), when false this will not be done.
* @type object * @type object
* @extends DataTable.models.oSearch * @extends DataTable.models.oSearch
* *

View File

@ -7,6 +7,13 @@
* @namespace * @namespace
*/ */
DataTable.models.oSearch = { DataTable.models.oSearch = {
/**
* Flag to indicate if the filtering should be case insensitive or not
* @type boolean
* @default true
*/
"bCaseInsensitive": true,
/** /**
* Applied search term * Applied search term
* @type string * @type string