Jon Harding
Jon Harding

Reputation: 4946

ng-repeat filtering based on array

I have a standard ng-repeat

<tr ng-repeat="tran in filteredTransactions(history)">

$scope.history is doing a call to a factory to get a json result back

$scope.history = historyFactory.getHistory;

I have created another scope object to compare items from an array and filter the $scope.history to build the ng-repeat

$scope.filteredTransactions = function () {
    if ($scope.filterBy.length > 1) {
        return $scope.history.filter(function (tran) {
            return $scope.filterBy.indexOf(tran.slot) !== -1;
        });
    }
    else {
        return $scope.history;
    }
}

I have a list of 'buttons' that users will be able to click to engage that item in the filter. For instance if they click 'Item 1' I would like that to be added to the array and the ng-repeat filtered accordingly.

I have an ng-click function that is pushing/popping the items out of the array correctly. However the ng-repeat isn't updating. Is there a better way to get this to work?

Upvotes: 0

Views: 204

Answers (1)

Scott
Scott

Reputation: 1690

Rather then try and return a list of filtered items use the filter and put your filter function there.

       <tr ng-repeat="tran in history | filter: filteredTransactions(tran)">

        $scope.filteredTransactions = function (item) {
         if ($scope.filterBy.length > 1) {
            // check criteria and return either true or false
         } else {
           return true;
         }

Upvotes: 1

Related Questions