K
K
Kirill Pisarev2015-08-31 22:14:03
JavaScript
Kirill Pisarev, 2015-08-31 22:14:03

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>

Above is a piece of code with 2 inputs where I enter the values ​​\u200b\u200bof the maximum and minimum prices, as well as ng-repeat itself with filters. Below is the function from the controller. console.log for debugging will display the desired values. I understand that the problem is hidden in that it is necessary to equate not to $scope.price, but I can’t understand why ...
$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

3 answer(s)
K
Kirill Pisarev, 2015-09-01
@P1sar

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">

A
Alex, 2015-09-01
@streetflush

You think correctly, try passing tablet to the filtering function

S
Sergey, 2015-08-31
Protko @Fesor

Stop trying to do everything in templates. Take out the logic at least in the controller (or rather, in the service, but I think it's too much to ask for this).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question