Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

have the previous item available in ng-repeat #10362

Closed
@digitalica

Description

@digitalica

I'm quite new to Angular, and was looking for a convenient way to create headers in a ng-repeat list (like first letter headers in a list of names, or month headers in a list of dates).

It occurred to me it would really help if the previous item would be available in the loop, so headers could be created by:

<html lang="en" ng-app="prevValueDemo">
<head>
    <script src="git/angular.js/build/angular.js"></script>
</head>
<body ng-controller="prevController">
    <p ng-repeat="nr in nrs">
        <!-- note: below is nr_prev the previous value now !!!! -->
        <span ng-if="(nr-nr%10)/10!=(nr_prev-nr_prev%10)/10" >
            <br><b>{{(nr-nr%10)/10}}0</b> <br>
        </span>
        <span>
            {{nr}}
        </span>
    </p>
    <script>
        var prevValueDemo = angular.module('prevValueDemo', []);

        prevValueDemo.controller('prevController', ['$scope', function($scope) {
            $scope.nrs = [12,14,15,22,23,45,46,76,78,79,80,83,99];
        }]);
    </script>
</body>
</html>

to get:

10

12
14
15

20

22
23

40

45
46

70

76
78
79

80

80
83

90

99

This proved to be quite simple to add to Angular. And it helps me in a project to create headers like this, and even totally different ones (or no headers at all) depending on the sorting. My change:
https://github.com/digitalica/angular.js/commit/7fce7d159b2d16280cbd844cc89360398a420093

I'm not so sure this fits the Angular way of doing things though. Especially at my simple choice of name (postfixing '_prev' to 'nr' in this case).

If it does, i'll run tests & checks, test it in combination with filtering and create a merge request. If it doesn't I'm open to suggestions for another approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions