angularjs search and ignore spanish characters -
i'm adding simple sort on page. idea search products. these products written in spanish language , has accents. example: 'jamón'.
here 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 problem have have type in "jamón" in order find product "jamón". want more flexible, if user types in "jamon", results must include "jamón".
how can search angular filters , forget accents? idea?
thanks in advance.
you'll need create filter function (or full filter). simplest thing 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']; };
jsfiddle here.
please notice works arrays of strings. if want filter list of objects (and search in every property of every object, angular does) you'll have enhance filter function. think example should started.
Comments
Post a Comment