Search code examples
javascripthtmljquerymodel-view-controllerdatatables

JQuery datatablejs turkish character search problem


I have a jquery datatablejs. I list items. I want to search something in search. But when i enter İ it finds only İ chars. I want to find also i chars. Like I-ı Ü-ü.

I searched lots of sites/docs but find anything.

This link below defines turkish chars for sorting. Can we use this for searching? Other problem is or we can say my constraint is I can't change datatable's original data. I can't replace İ to i or i to İ.

https://datatables.net/plug-ins/sorting/turkish-string

Note: I am getting data with ajax from mvc controller. I do not get json data. I get model object.


Solution

  • I found a solution and it work without any problem.

    Here is the solution:

    I call this function:

    https://jsfiddle.net/s39o8pdu/1/

    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
      'locale-compare-asc': function ( a, b ) {
         return a.localeCompare(b, 'cs', { sensitivity: 'case' })
      },
      'locale-compare-desc': function ( a, b ) {
         return b.localeCompare(a, 'cs', { sensitivity: 'case' })
      }
    })
    
    jQuery.fn.dataTable.ext.type.search['locale-compare'] = function (data) {
        return NeutralizeAccent(data);
    }
    
    function NeutralizeAccent(data)
    {
      return !data
          ? ''
            : typeof data === 'string'
            ? data
            .replace(/\n/g, ' ')
            .replace(/[éÉěĚèêëÈÊË]/g, 'e')
            .replace(/[šŠ]/g, 's')
            .replace(/[čČçÇ]/g, 'c')
            .replace(/[řŘ]/g, 'r')
            .replace(/[žŽ]/g, 'z')
            .replace(/[ýÝ]/g, 'y')
            .replace(/[áÁâàÂÀ]/g, 'a')
            .replace(/[íÍîïÎİÏ]/g, 'i')
            .replace(/[ťŤ]/g, 't')
            .replace(/[ďĎ]/g, 'd')
            .replace(/[ňŇ]/g, 'n')
            .replace(/[óÓ]/g, 'o')
            .replace(/[úÚůŮ]/g, 'u')
            : data
    }
    
    var table = $('#example').DataTable({
        columnDefs : [
         { targets: 0, type: 'locale-compare' },
      ]
    })
    
     $('#example_filter input').keyup(function () {
        table
        .search(
          jQuery.fn.dataTable.ext.type.search.string(NeutralizeAccent(this.value))
        )
        .draw()
     })
     
    

    So good luck who has this problem. If you have any problem ask me. I know how to solve this problem now.

    Like andrewjames said on the answers we solve this problem with Accent neutralise.