user1679941
user1679941

Reputation:

How can I limit rows showing in an ng-repeat?

I have an ng-repeat that looks like this:

 <div ng-repeat="subject in subjects">
    <div>{{ subject.name }}</div>
 </div>

Is there some way that I can limit the display of rows to rows where subject.id is less than 50 if roleId > 1 and otherwise show all rows?

$scope.roleId = 0 means no filter $scope.roleId > 0 means filter to show only rows in $scope.subjects where subject.id is less than 50

Upvotes: 3

Views: 1672

Answers (3)

Francis Ducharme
Francis Ducharme

Reputation: 4987

Add a filter

<div ng-repeat="subject in subjects | filter:filterFn">
    <div>{{ subject.name }}</div>
</div>

And in your controller, have this function:

$scope.filterFn = function (item) {
    if ($scope.roleId > 1) {
        return (item.Id < 50) ? true : false;
    }
    else
        return true;
};

If I understood your comments correctly.

Upvotes: 0

Joe Lloyd
Joe Lloyd

Reputation: 22333

Use Limit to Filter

<div ng-repeat="subject in subjects | limitTo:quantity">
    <div>{{ subject.name }}</div>
</div>

then set the $scope.quantity in your controller based on your business logic.

Use a Custom Filter

to use a custom filter you just set a new $scope function in your controller.

$scope.yourFilter = function (items) {
    // probably need some more logic but you get the idea
    return items.id < 50;
};

thin in the filter it should look like this

<div ng-repeat="subject in subjects | filter:yourFilter(subjects)">
    <div>{{ subject.name }}</div>
</div>

Upvotes: 2

charlietfl
charlietfl

Reputation: 171679

You can accomplish this using ng-if or use a filter

 <div ng-repeat="subject in subjects" ng-if="subject.id>50">

The part about the role id in question is not clear

Upvotes: 4

Related Questions