Search code examples
angularjslistvisibleselectall

AngularJS - "Select All" items that are currently visible


I am currently trying to figure out the best way to select all items in a list that are currently visible.

I currently have a large list of items in my scope that has paging applied to it so only a few items of this list are visible at a time. I have a "Select All" button where the desired behavior is to have it select all the items that are currently visible - not all the items in the list.

I think I can achieve it by using the ng-init directive to add visible items to a list in the controller, I can then use that list to see what is visible. To me it seems that there has to be a better solution that I am missing.

Does anyone have a elegant solution to this?


Solution

  • Not the clearest of questions, but I assume you're using an ng-repeat with some sort of filter that's knocking down the items to only the ones you want to show. You can set an inline scope variable when declaring your ng-repeat and work off of that.

    So if your html looks like this:

    <div ng-repeat="item in items | someFilter"></div>
    

    You can change it to:

    <div ng-repeat="item in visibleItems = (items | someFilter)"></div>
    

    Then you can use $scope.visibleItems inside your controller and it will only contain the certain subset of items that have passed your someFilter.