Accents neutralise - DataTable

Example of how use the accents neutralise DataTable plugin

by Gustavo Smaniotto

HTML

<script src="https://cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.7/css/jquery.dataTables.css">
<table id="people-data">
  <thead>
    <tr>
      <th>Nome</th>
      <th>Idade</th>
      <th>Comida Preferida</th>
      <th>Profiss&atilde;o</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <p>Jo&atilde;o</p>
      </td>
      <td>21</td>
      <td>&nbsp;Lasanha</td>
      <td>&nbsp;Estudante</td>
    </tr>
    <tr>
      <td>
        <p>&nbsp;Gustavo</p>
      </td>
      <td>&nbsp;22</td>
      <td>Pizza&nbsp;</td>
      <td>T&eacute;cnico agr&iacute;cola&nbsp;</td>
    </tr>
    <tr>
    <td>T&aacute;cila&nbsp;</td>
    <td>15&nbsp;</td>
    <td>Picol&eacute;</td>
    <td>Estagi&aacute;ria&nbsp;</td>
    </tr>
    <tr>
    <td>Yohana&nbsp;</td>
    <td>20&nbsp;</td>
    <td>Sorvete&nbsp;</td>
    <td>Engenheira de produ&ccedil;&atilde;o&nbsp;</td>
    </tr>
    <tr>
    <td>Marcil&eacute;ia&nbsp;</td>
    <td>31&nbsp;</td>
    <td>Mam&atilde;o pap&aacute;ia</td>
    <td>Professora&nbsp;</td>
    </tr>
    <tr>
    <td>J&eacute;ferson&nbsp;</td>
    <td>34&nbsp;</td>
    <td>Churrasco&nbsp;</td>
    <td>Agr&ocirc;nomo&nbsp;</td>
    </tr>
    <tr>
    <td>Ant&ocirc;nio&nbsp;</td>
    <td>5&nbsp;</td>
    <td>Pur&ecirc; de batata&nbsp;</td>
    <td>Desempregado</td>
    </tr>
    <tr>
    <td>Ana&nbsp;</td>
    <td>21&nbsp;</td>
    <td>&nbsp;P&ecirc;ra</td>
    <td>&nbsp;Engenheira</td>
    </tr>
  </tbody>
</table>

JavaScript

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, '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');
    }

  
  var searchType = jQuery.fn.DataTable.ext.type.search;

  searchType.string = function (data) {
    return !data ?
      '' :
      typeof data === 'string' ?
        removeAccents(data) :
        data;
  };

  searchType.html = function (data) {
    return !data ?
      '' :
      typeof data === 'string' ?
        removeAccents(data.replace(/<.*?>/g, '')) :
        data;
  };

  jQuery('#people-data').DataTable();

  jQuery(document).ready(function () {
      var table = $('#people-data').DataTable();

      // Remove accented character from search input as well
      jQuery('#people-data_filter input').keyup(function () {
        table
          .search(
          $.fn.DataTable.ext.type.search.string(this.value)
          )
          .draw()
      });
    });