angularjs搜索和忽略西班牙字符 [英] angularjs search and ignore spanish characters

查看:127
本文介绍了angularjs搜索和忽略西班牙字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加一个页面上简单的排序。我们的想法是要搜索的产品。这些产品都写在西班牙语并且有口音。例如:火腿

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'.

下面是我的code:

<div class="form-inline">
  <label>Search</label>
  <input type="text" ng-model="q"/>
</div>

<div ng-repeat="product in products_filtered = (category.products | filter:q | orderBy:'name')">
           ....
</div>

我唯一的问题是,你有为了找到产品的火腿输入火腿。我要的是更加灵活,如果火腿的用户类型,结果必须有火腿。

The only problem i have is that you have to type in "Jamón" in order to find the product "Jamón". What I want is to be more flexible, if the user types in "Jamon", the results must include "Jamón".

如何搜索具有角器和忘记口音?任何想法?

How can i search with angular filters and forget about accents? Any Idea?

先谢谢了。

推荐答案

您需要创建一个过滤函数(或一个完整的过滤器)。这是能够工作的最简单的事情:

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

HTML

<div ng-app ng-controller="Ctrl">
    <input type="text" ng-model="search">
    <ul>
        <li ng-repeat="name in names | filter:ignoreAccents">{{ name }}</li>
    </ul>
</div>

的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'];
};

请注意,这仅适用于字符串的阵列。如果您要筛选对象的列表(在每个对象的每个属性进行搜索,像角一样),你就必须增强过滤功能。我觉得这个例子应该让你开始。

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.

这篇关于angularjs搜索和忽略西班牙字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆