I am new to Angular and I need your help writing a custom filter where I can search through all array objects and get my matching results that would ignore period(.) and commas(,). For example, if I type "ashton," I should get "stash" and "ashton marton". But I am at a loss and need some advice. Here's what I have so far.
In my angularSearch.html I have this.
<!DOCTYPE html>
<html>
<head>
<script src="js/angular.js"></script>x
<title></title>
</head>
<body ng-app="storeSearch">
<div ng-controller="searchController">
<label>Search: </label>
<input type="text" class="search" ng-model="searchString" placeholder="Search"/>
<table>
<tr ng-repeat="p in products | searchFilter: searchString">
<td>{{p.product_code}}</td>
<td>{{p.subname}}</td>
<td>{{p.description}}</td>
</tr>
</table>
</div>
<script src="app/app.js"></script>
</body>
</html>
My app.js is shown below.
var app = angular.module("storeSearch", []);
app.controller("searchController", ["$scope", function($scope){
$scope.products = [
{
product_title: "A-2368",
description:"Asthon Martin",
sub_name:"2000 model ashton martin"
},
{
product_title:"S-2310",
description:"Stash of money",
sub_name:"Rolls Royce"
},
{
product_title:"h-2369",
description:"Honda Civic 2003",
sub_name:"Marton Ashton, Honda Series"
}
];
}]);
app.filter('searchFilter', function(){
return function(arr, searchString){
if(!searchString){
return arr;
}
var result = [];
searchString = searchString.toLowerCase();
angular.forEach(arr, function(products){
if(products.value.toLowerCase().indexOf(searchString) !== -1){
result.push(products);
}
});
return result;
};
});
ashton
?