Answer the question
In order to leave comments, you need to log in
How to filter values received from ng-repeat?
Good afternoon. There is an ng-repeat tag that prints values from an object. I want to make it possible to filter by two values (in this case, by the minimum and maximum price)
<th>
<label for="priceMin">Цена от:</label>
<input id = 'priceMin' class="form-control" type="text" ng-model="priceRangeMin" />
<label for="priceMax">до:</label>
input id = 'priceMax' class="form-control" type="text" ng-model="priceRangeMax" />
</th>
</tr>
</thead>
<tr ng-repeat="tablet in tablets | filter:priceRangeFilter(priceRangeMin, priceRangeMax)| orderBy:choosedFilter">
<td>{{$index + 1}}</td>
<td>{{tablet.name}}</td>
<td>{{tablet.price | currency}}</td>
<td>{{tablet.year}}</td>
<td>{{tablet.company}}</td>
<td>{{tablet.rate}}</td>
$scope.priceRangeFilter = function(priceRangeMin, priceRangeMax){
if (priceRangeMax === undefined) priceRangeMax = 1;
if(priceRangeMin === undefined) priceRangeMin = 1;
console.log(priceRangeMax);
console.log(priceRangeMin);
return $scope.price >= priceRangeMin
};
Answer the question
In order to leave comments, you need to log in
In general, I googled and did so. Created a new filter in the controller and added it to the template. Everything works ... Maybe someone will come in handy.
myApp.filter('priceRangeFilter', function () {
return function (items, priceRangeMin, priceRangeMax) {
if (priceRangeMax === undefined) priceRangeMax = 999999;
if (priceRangeMin === undefined) priceRangeMin = 0;
var filtered = [];
for (var i = 0; i < items.length; i++){
var item = items[i];
if (item.price >= +priceRangeMin){
if(item.price <= +priceRangeMax){
filtered.push(item);
}
}
}
return filtered
};
});
<tr ng-repeat="tablet in tablets | priceRangeFilter:priceRangeMin:priceRangeMax| orderBy:choosedFilter">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question