filtering spanish accents in searching box

filtering spanish accents in searching box

bucanerobucanero Posts: 15Questions: 2Answers: 0
edited May 2014 in DataTables

Hi, I got a problem like this: http://www.datatables.net/forums/discussion/18763/key-problems-with-accent-spanish

But I need to ignore any accent at search (like google).

When I write Pérez, Perez or Pèrez (one of them) it'd return all (Juan Perez, Jose Pérez and Pedro Pèrez)

Do you understand? (I'm spanish, sorry my english)

Thanks and regards.

Answers

  • allanallan Posts: 50,060Questions: 1Answers: 7,409 Site admin
  • bucanerobucanero Posts: 15Questions: 2Answers: 0
    edited May 2014

    Thanks for your quick answer, but I've tested it and doesn't work.

    When we want to write a accented letter, we've to push two keys: one for accent (doesn't write anything) and another for letter.

    Then replace() searchs the first one (the accent) and don't search the second (the letter)

    I think that these is the problem.

  • allanallan Posts: 50,060Questions: 1Answers: 7,409 Site admin

    Can you link to a page showing the problem so I can see it and try to offer some help then.

    Allan

  • bucanerobucanero Posts: 15Questions: 2Answers: 0
    edited May 2014

    No, I can't. For several reasons. :-(

    Please, Would you test it with the example of the link that I put avobe?

    Thanks a lot.

  • allanallan Posts: 50,060Questions: 1Answers: 7,409 Site admin

    I don't have any spare time at the moment. I'll try to make some early next week, but can't promise anything. If you can, please use http://live.datatables.net or JSFiddle to recreate the issue so I can help.

    Allan

  • bucanerobucanero Posts: 15Questions: 2Answers: 0

    Ok, When I'll can, I do it. Thanks for all

  • bucanerobucanero Posts: 15Questions: 2Answers: 0
    edited May 2014

    Hi,
    ...and the solution is:

    1.-SCRIPT:

    function accents_supr(data){
        return ! data ?
            '' :
            typeof data === 'string' ? 
                data
                    .replace( /\n/g, ' ' )
                    .replace( /[áàäâ]/g, 'a' )
                    .replace( /[éèëê]/g, 'e' )
                    .replace( /[íìïî]/g, 'i' )
                    .replace( /[óòöô]/g, 'o' )
                    .replace( /[úùüû]/g, 'u' ):
                data;
    
    jQuery.extend( jQuery.fn.dataTableExt.oSort,
    {
        "spanish-string-asc"  : function (s1, s2) { return s1.localeCompare(s2); },
        "spanish-string-desc" : function (s1, s2) { return s2.localeCompare(s1); }
    });
    
    jQuery.fn.DataTable.ext.type.search['spanish-string'] = function ( data ) {
        return accents_supr(data);        
    }
    

    2.-IN FILE (jquery.datatables.js) MODIFY in function _fnFeatureHtmlFilter() :

    var searchFn = function() {
            /* Update all other filter input elements for the new display */
            var n = features.f;
            var val = !this.value ? "" : this.value; // mental IE8 fix :-(
    
            /* Now do the filter */
            if ( val != previousSearch.sSearch ) {
                    
            /********************
            **** MY CHANGE ******
            ********************/
            val=accents_supr(val);
            /*******************/
            
                    _fnFilterComplete( settings, {
                            "sSearch": val,
                            "bRegex": previousSearch.bRegex,
                            "bSmart": previousSearch.bSmart ,
                            "bCaseInsensitive": previousSearch.bCaseInsensitive
                    } );
    
                    // Need to redraw, without resorting
                    settings._iDisplayStart = 0;
                    _fnDraw( settings );
            }
    };
    

    If you'll modify jquery.datatables.js for his distribution, please you'd tell me.

    Thanks.

This discussion has been closed.