angularjs search and ignore spanish characters

前端 未结 5 1934
名媛妹妹
名媛妹妹 2020-12-15 14:03

I\'m adding a simple sort on a page. The idea is to search products. These products are written in spanish language and has accents. For example: \'Jamón\'.

Here is

5条回答
  •  萌比男神i
    2020-12-15 14:50

    Here is a slightly enhanced version (not counting the extra foreign characters it searches for, of course) of the above. I put it straight into my controller and it allowed me to search all the data that I wanted to search. Thanks Bernardo for your input into this!

    Hope it helps somebody out.

     function removeAccents(value) {
        return value
             .replace(/á/g, 'a') 
             .replace(/â/g, 'a')            
             .replace(/é/g, 'e')
             .replace(/è/g, 'e') 
             .replace(/ê/g, 'e')
             .replace(/í/g, 'i')
             .replace(/ï/g, 'i')
             .replace(/ì/g, 'i')
             .replace(/ó/g, 'o')
             .replace(/ô/g, 'o')
             .replace(/ú/g, 'u')
             .replace(/ü/g, 'u')
             .replace(/ç/g, 'c')
             .replace(/ß/g, 's');
    }
    
    $scope.ignoreAccents = function (item) {
    if ($scope.search) {
    var search = removeAccents($scope.search).toLowerCase();
    var find = removeAccents(item.name + ' ' + item.description+ ' ' + item.producer+ ' ' +        item.region).toLowerCase();
    return find.indexOf(search) > -1;
    }
    return true;
    };
    

提交回复
热议问题