Filtering - improved support for searching with accents

- Thanks to lledieu for pointing this out
pull/447/head
Allan Jardine 6 years ago
parent 12d60fe7ab
commit 9e6ea96aa6

@ -4,11 +4,9 @@
* type based search plug-in replaces the built-in string formatter in * type based search plug-in replaces the built-in string formatter in
* DataTables with a function that will replace the accented characters * DataTables with a function that will replace the accented characters
* with their unaccented counterparts for fast and easy filtering. * with their unaccented counterparts for fast and easy filtering.
* *
* Note that with the accented characters being replaced, a search input using * Note that this plug-in uses the Javascript I18n API that was introduced in
* accented characters will no longer match. The second example below shows * ES6. For older browser's this plug-in will have no effect.
* how the function can be used to remove accents from the search input as well,
* to mitigate this problem.
* *
* @summary Replace accented characters with unaccented counterparts * @summary Replace accented characters with unaccented counterparts
* @name Accent neutralise * @name Accent neutralise
@ -18,54 +16,23 @@
* $(document).ready(function() { * $(document).ready(function() {
* $('#example').dataTable(); * $('#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(){
function removeAccents ( data ) { function removeAccents ( data ) {
return data if ( data.normalize ) {
.replace( /έ/g, 'ε' ) // Use I18n API if avaiable to split characters and accents, then remove
.replace( /[ύϋΰ]/g, 'υ' ) // the accents wholesale. Note that we use the original data as well as
.replace( /ό/g, 'ο' ) // the new to allow for searching of either form.
.replace( /ώ/g, 'ω' ) return data +' '+ data
.replace( /ά/g, 'α' ) .normalize('NFD')
.replace( /[ίϊΐ]/g, 'ι' ) .replace(/[\u0300-\u036f]/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' );
} }
return data;
}
var searchType = jQuery.fn.DataTable.ext.type.search; var searchType = jQuery.fn.DataTable.ext.type.search;
searchType.string = function ( data ) { searchType.string = function ( data ) {

Loading…
Cancel
Save