Data table with accents example
An intent of solution for accents in two ways when searching accent words and no accent words.
HTML
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/1.0.6/js/dataTables.responsive.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.css">
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/1.0.6/css/dataTables.responsive.css">
<table id="datatable-table">
<thead>
<tr>
<th>Object</th>
<th>Word</th>
<th>Thing</th>
<th>Class</th>
<th>Think</th>
</tr>
</thead>
<tbody>
<tr>
<td>árbol</td>
<td>Esdrújula</td>
<td>Colaboración</td>
<td>Pingüino</td>
<td>Canción</td>
</tr>
<tr>
<td>arbol</td>
<td>Esdrujula</td>
<td>Colaboracion</td>
<td>Pinguino</td>
<td>cancion</td>
</tr>
<tr>
<td>Música</td>
<td>Nota</td>
<td>Melodia</td>
<td>Instrumento</td>
<td>Pieza</td>
</tr>
<tr>
<td>Music</td>
<td>Note</td>
<td>Melody</td>
<td>Instrument</td>
<td>Piece</td>
</tr>
</tbody>
</table>
<p>Using:<br/>
jQuery v: 2.1.0<br/>
jquery.dataTables.min.js v: 1.10.7<br/>
dataTables.responsive.min.js v: 1.0.6<br/>
The idea is concatenate OR's of both, the accent and no accent word. In that way if you search "pingüino" or "pinguino", it bring both results in any case.
</p>
JavaScript
jQuery.fn.DataTable.ext.type.search.string = function ( data ) {
return ! data ?
'' :
typeof data === 'string' ?
data
.replace( /έ/g, 'ε')
.replace( /ύ/g, 'υ')
.replace( /ό/g, 'ο')
.replace( /ώ/g, 'ω')
.replace( /ά/g, 'α')
.replace( /ί/g, 'ι')
.replace( /ή/g, 'η')
.replace( /\n/g, ' ' )
.replace( /[áÁ]/g, 'a' )
.replace( /[éÉ]/g, 'e' )
.replace( /[íÍ]/g, 'i' )
.replace( /[óÓ]/g, 'o' )
.replace( /[úÚ]/g, 'u' )
.replace( /ê/g, 'e' )
.replace( /î/g, 'i' )
.replace( /ô/g, 'o' )
.replace( /è/g, 'e' )
.replace( /ï/g, 'i' )
.replace( /ü/g, 'u' )
.replace( /ã/g, 'a' )
.replace( /õ/g, 'o' )
.replace( /ç/g, 'c' )
.replace( /ì/g, 'i' ) :
data;
};
var tabla = jQuery('#datatable-table').dataTable( { } );
new jQuery.fn.dataTable.Responsive( tabla );
jQuery(document).ready(function() {
var table = jQuery('#datatable-table').DataTable();
// Remove accented character from search input as well
jQuery('#datatable-table_filter input').keyup( function () {
table
.search(
jQuery.fn.DataTable.ext.type.search.string( this.value )
)
.draw();
} );
} );