zmogusnamas
zmogusnamas

Reputation: 61

AngularJs search and ignoring diacritics

My idea is to have a list of words in which there are lots of diacritics or accents. And for example instead of writing Róse to have Róse I'd like to write Rose and have Róse in results. First of all I googled for it, here in StackOverflow the problem was partially solved.

But what if I have array like this, check jsFiddle:

     $scope.names = [{name: 'Jamón', surname: 'Géroux'},
    {name: 'Andrés', surname: 'Guérin'},
    {name: 'Cristián', surname: 'Róse'},
    {name: 'Fernán', surname:'Raúlien'}];
    };

Then the solution doesn't work: jsFiddle. And what if I had, for example, custom filter for highlight:

     <td ng-bind-html="name.name | highlight:search.$"></td>

I've fount out, that all examples here https://builtwith.angularjs.org have the same problem with diacritics/accents. And only one website there http://illicoweb.videotron.com uses "simple technics" with JavaScript function and str.replace.

Any ideas on really simple and fast solution? Without storing two tables with diacritics/accents and without it.

Thanks in advance!

Upvotes: 1

Views: 2990

Answers (1)

John Stafford
John Stafford

Reputation: 101

I've updated this JSFiddle with my modifications to your filter. I've simply modified the filter to search the full name rather than just the first name as demonstrated:

$scope.ignoreAccents = function(item) {               
    if (!$scope.search)
        return true;

    var fullName = item.name + ' ' + item.surname;
    var text = removeAccents(fullName.toLowerCase());
    var search = removeAccents($scope.search.toLowerCase());
    return text.indexOf(search) > -1;
};

This approach should hopefully negate any need for two separate tables.

Upvotes: 4

Related Questions