javascriptjquerydatatablesaccent-insensitive

jQuery DataTables not working special characters results in search


I am trying to search some words with special characters in jQuery Datatables plugin.

There are some results in datatable like this:

Peinado, Alma_María
Aguilar Castillo, Antonio José

When I try to search "alma_maría", the first result is shown:

Peinado, Alma_María

When I try "alma_maria" (note that I am using character "i" instead "í"), nothing is shown.

Screenshot 1:

Trying to search "alma_maría"

Screenshot 2:

Trying to search "alma_maria"

Is there any way to configure Datatables to show special characters results when I search without special characters?

My HTML/Javascript code:

<table class="display table table-bordered table-striped" id="table-colegiados">
<thead>
<tr>
    <th>{$TXT.nombre}</th>
    <th>{$TXT.ciudad}</th>
    <th>{$TXT.email}</th>
    <th>{$TXT.telefono}</th>
    <th>{$TXT.dni}</th>
    <th>{$TXT.colegiado}</th>
    <th>{$TXT.asesor}</th>
    <th>{$TXT.editar}</th>
    <th>{$TXT.borrar}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<script type="text/javascript">
    $.fn.dataTableExt.oApi.fnGetFilteredNodes = function ( oSettings )
    {
        var anRows = [];
        for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ) {
            var nRow = oSettings.aoData[ oSettings.aiDisplay[i] ].nTr;
            anRows.push( nRow );
        }
        return anRows;
    };
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
        if ( oSettings.nTable == document.getElementById( 'table-colegiados' )){
            var asesor = aData[6];
            var checked = $('#checkbox_asesores').is(':checked');
            if (checked) {
                if (asesor == '1') {
                    return true;
                }
            } else {
                return true;
            }
            return false;
        } else {
            return true;
        }
    });
    var oTable = $('#table-colegiados').dataTable({
            "aaSorting": [ [0,'asc'] ],
            'iDisplayLength': 25,
            "bProcessing": true,
            "bServerSide": false,
            "bDeferRender": true,
            "sAjaxSource": "ajax/getColegiados.php",
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                $.getJSON( sSource, aoData, function (json) {
                    fnCallback(json)
                } );
            }
    });
    $('#checkbox_asesores').on("click", function(e) {
        oTable.fnDraw();
    });
</script>

Solution

  • I don't know why, but "Accent Neutralisation" is not working for me.

    I had a creative idea that worked like a charm.

    I used in the PHP side a function like this:

    public function cleanString($String)
    {
        $String = str_replace(array('á','à','â','ã','ª','ä'), "a", $String);
        $String = str_replace(array('Á','À','Â','Ã','Ä'), "a", $String);
        $String = str_replace(array('Í','Ì','Î','Ï'), "i", $String);
        $String = str_replace(array('í','ì','î','ï'), "i", $String);
        $String = str_replace(array('é','è','ê','ë'), "e", $String);
        $String = str_replace(array('É','È','Ê','Ë'), "e", $String);
        $String = str_replace(array('ó','ò','ô','õ','ö','º'), "o", $String);
        $String = str_replace(array('Ó','Ò','Ô','Õ','Ö'), "o", $String);
        $String = str_replace(array('ú','ù','û','ü'), "u", $String);
        $String = str_replace(array('Ú','Ù','Û','Ü'), "u", $String);
        $String = str_replace(array('[','^','´','`','¨','~',']'), "", $String);
        $String = str_replace("ç", "c", $String);
        $String = str_replace("Ç", "C", $String);
        $String = str_replace("ñ", "n", $String);
        $String = str_replace("Ñ", "N", $String);
        $String = str_replace("Ý", "Y", $String);
        $String = str_replace("ý", "y", $String);
        $String = str_replace("&aacute;", "a", $String);
        $String = str_replace("&Aacute;", "a", $String);
        $String = str_replace("&eacute;", "e", $String);
        $String = str_replace("&Eacute;", "e", $String);
        $String = str_replace("&iacute;", "i", $String);
        $String = str_replace("&Iacute;", "i", $String);
        $String = str_replace("&oacute;", "o", $String);
        $String = str_replace("&Oacute;", "o", $String);
        $String = str_replace("&uacute;", "u", $String);
        $String = str_replace("&Uacute;", "u", $String);
        return $String;
    }
    

    To replace special chars with its non-special-char version.

    Then added to the JSON, in the "name" field, this non-specialchars version inside a HTML comment.

    I had:

    $arrayJson = array();
    foreach ($arrayUsers as $item) {
        $arrayJson[] = array(
            $item['name'],
            $item['city'],
            $item['email'],
            $item['phone'],
            $item['id'],
            $item['colleged'],
            $item['asesor']
        );
    }
    $jsonStr = json_encode($arrayJson);
    

    Now I have:

    $arrayJson = array();
    foreach ($arrayUsers as $item) {
        $cleanName = $Utils->cleanString($item['name']);
        $arrayJson[] = array(
            '<!-- ' . $cleanName . ' -->' . $item['name'],
            $item['city'],
            $item['email'],
            $item['phone'],
            $item['id'],
            $item['colleged'],
            $item['asesor']
        );
    }
    $jsonStr = json_encode($arrayJson);