angularjs search and ignore spanish characters

前端 未结 5 1930
名媛妹妹
名媛妹妹 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条回答
  •  伪装坚强ぢ
    2020-12-15 15:01

    You'll need to create a filter function (or a full filter). This is the simplest thing that could possibly work:

    HTML

    • {{ name }}

    Javascript

    function Ctrl($scope) {
        function removeAccents(value) {
            return value
                .replace(/á/g, 'a')            
                .replace(/é/g, 'e')
                .replace(/í/g, 'i')
                .replace(/ó/g, 'o')
                .replace(/ú/g, 'u');
        }
    
        $scope.ignoreAccents = function(item) {               
            if (!$scope.search) return true;       
    
            var text = removeAccents(item.toLowerCase())
            var search = removeAccents($scope.search.toLowerCase());
            return text.indexOf(search) > -1;
        };
    
        $scope.names = ['Jamón', 'Andrés', 'Cristián', 'Fernán', 'Raúl', 'Agustín'];
    };
    

    jsFiddle here.

    Please notice that this only works for arrays of strings. If you want to filter a list of objects (and search in every property of every object, like Angular does) you'll have to enhance the filter function. I think this example should get you started.

提交回复
热议问题