From 9e6ea96aa650b69cbd7162f56f68fdb1df3b7c8f Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Mon, 27 May 2019 11:08:43 +0000 Subject: [PATCH] Filtering - improved support for searching with accents - Thanks to lledieu for pointing this out --- filtering/type-based/accent-neutralise.js | 59 +++++------------------ 1 file changed, 13 insertions(+), 46 deletions(-) diff --git a/filtering/type-based/accent-neutralise.js b/filtering/type-based/accent-neutralise.js index 806bbd9..69eb1e8 100644 --- a/filtering/type-based/accent-neutralise.js +++ b/filtering/type-based/accent-neutralise.js @@ -4,11 +4,9 @@ * type based search plug-in replaces the built-in string formatter in * DataTables with a function that will replace the accented characters * with their unaccented counterparts for fast and easy filtering. - * - * Note that with the accented characters being replaced, a search input using - * accented characters will no longer match. The second example below shows - * how the function can be used to remove accents from the search input as well, - * to mitigate this problem. + * + * Note that this plug-in uses the Javascript I18n API that was introduced in + * ES6. For older browser's this plug-in will have no effect. * * @summary Replace accented characters with unaccented counterparts * @name Accent neutralise @@ -18,54 +16,23 @@ * $(document).ready(function() { * $('#example').dataTable(); * } ); - * - * @example - * $(document).ready(function() { - * var table = $('#example').dataTable(); - * - * // Remove accented character from search input as well - * $('#myInput').keyup( function () { - * table - * .search( - * jQuery.fn.DataTable.ext.type.search.string( this.value ) - * ) - * .draw() - * } ); - * } ); */ (function(){ function removeAccents ( data ) { - return data - .replace( /έ/g, 'ε' ) - .replace( /[ύϋΰ]/g, 'υ' ) - .replace( /ό/g, 'ο' ) - .replace( /ώ/g, 'ω' ) - .replace( /ά/g, 'α' ) - .replace( /[ίϊΐ]/g, 'ι' ) - .replace( /ή/g, 'η' ) - .replace( /\n/g, ' ' ) - .replace( /[ÀÁÂÃÄÅ]/g, 'A' ) - .replace( /[àáâãäå]/g, 'a' ) - .replace( /[ÈÉÊË]/g, 'E' ) - .replace( /[èéêë]/g, 'e' ) - .replace( /[ÌÍÎÏ]/g, 'i' ) - .replace( /[ìíîï]/g, 'i' ) - .replace( /[ÒÓÔÕÖ]/g, 'O' ) - .replace( /[òóôõö]/g, 'o' ) - .replace( /[ÙÚÛÜ]/g, 'U' ) - .replace( /[ùúûü]/g, 'u' ) - .replace( /Ñ/g, 'N' ) - .replace( /ñ/g, 'n' ) - .replace( /Ț/g, 'T' ) - .replace( /ț/g, 't' ) - .replace( /Ș/g, 'S' ) - .replace( /ș/g, 's' ) - .replace( /Ç/g, 'C' ) - .replace( /ç/g, 'c' ); + if ( data.normalize ) { + // Use I18n API if avaiable to split characters and accents, then remove + // the accents wholesale. Note that we use the original data as well as + // the new to allow for searching of either form. + return data +' '+ data + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, ''); } + return data; +} + var searchType = jQuery.fn.DataTable.ext.type.search; searchType.string = function ( data ) {