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

New: Smart filtering upgraded to support quoted exact match

- DataTables' smart filtering now has the ability to search for an exact
  string, in the given order, over multiple words. This is done by
  providing the exact match search string in quotes - for example:

    Angela "Cheif Exec" London

  Will match three terms - `Angela`, `Cheif Exec` and `London`. Those
  three terms will be matched in any order.

- This matches the search behaviour that we all know and love in Google!

- It can be disabled along with the rest of DataTables' smart filtering
  using the `smart` option.

- This enhancement comes courtesy of Barry Keepence. Original forum
  thread: http://datatables.net/forums/discussion/16250/. Thanks!
This commit is contained in:
Allan Jardine 2014-03-25 09:49:40 +00:00
parent c5058c36e6
commit af7f2780f4
2 changed files with 21 additions and 12 deletions

View File

@ -1 +1 @@
caffdb49eec87db2e3c1999eaec6736257b8f6b5
f21e58d17a7dba6644b7fb8381354fa351043017

View File

@ -2797,21 +2797,30 @@
* @returns {RegExp} constructed object
* @memberof DataTable#oApi
*/
function _fnFilterCreateSearch( sSearch, bRegex, bSmart, bCaseInsensitive )
function _fnFilterCreateSearch( search, regex, smart, caseInsensitive )
{
var asSearch,
sRegExpString = bRegex ? sSearch : _fnEscapeRegex( sSearch );
if ( bSmart )
{
/* Generate the regular expression to use. Something along the lines of:
* ^(?=.*?\bone\b)(?=.*?\btwo\b)(?=.*?\bthree\b).*$
search = regex ?
search :
_fnEscapeRegex( search );
if ( smart ) {
/* For smart filtering we want to allow the search to work regardless of
* word order. We also want double quoted text to be preserved, so word
* order is important - a la google. So this is what we want to
* generate:
*
* ^(?=.*?\bone\b)(?=.*?\btwo three\b)(?=.*?\bfour\b).*$
*/
asSearch = sRegExpString.split( ' ' );
sRegExpString = '^(?=.*?'+asSearch.join( ')(?=.*?' )+').*$';
var a = $.map( search.match( /"[^"]+"|[^ ]+/g ) || '', function ( word ) {
return word.charAt(0) === '"' ?
word.match( /^"(.*)"$/ )[1] :
word;
} );
search = '^(?=.*?'+a.join( ')(?=.*?' )+').*$';
}
return new RegExp( sRegExpString, bCaseInsensitive ? "i" : "" );
return new RegExp( search, caseInsensitive ? 'i' : '' );
}