0

I am looking for a way to count the amount of 'td' elements created in a loop.

I can't use $index for this as on each row the index gets reset, what is the cleanest and simplest way to to set i on each iteration. So the first column value is 1 & the first column count 0.

My Code so far:

        <table class="calendar">
            <thead>
                <tr>
                    <th>M</th>
                    <th>T</th>
                    <th>W</th>
                    <th>T</th>
                    <th>F</th>
                    <th>S</th>
                    <th>S</th>
                </tr>
            </thead>
            <tbody ng-click="bindCellValue($event)">
                <tr ng-repeat="week in (days.length/7 | array)">
                    <td ng-repeat="day in days.slice(7*$index, 7*$index + 7) track by $index">
                        {{ day }}
                        <i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[i].correct"></i>
                        <i class="icon ion-close answer-wrong" ng-if="submitted && !answers[i].correct"></i>
                    </td>
                </tr>

            </tbody>
        </table>

and in my controller:

$scope.days = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, null, null, null, null ];
ChrisBratherton
  • 1,540
  • 6
  • 26
  • 63
  • You mean `days.length`? Anyway, you know the number beforehand. It's based on the model, so why count something? – a better oliver Sep 03 '15 at 13:09
  • 1
    because I need to use the count of the items to match up with the key in an answers array: ng-if="submitted && answers[i].correct"> – ChrisBratherton Sep 03 '15 at 13:12
  • Can you create a plunker or jsfiddle for the same? – Vipul Agarwal Sep 03 '15 at 13:13
  • I'm not quite sure I get the question. Do you want the current count for each cell? Can you maybe draw or make a quick static table of how you want it to function? – Phillip Sep 03 '15 at 13:15
  • Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope.So what you need to do is reach up to the parent scope, and use that $index. See [this](http://stackoverflow.com/questions/15256600/passing-2-index-values-within-nested-ng-repeat) – road2victory Sep 03 '15 at 13:33

2 Answers2

2

You can use ng-init.

<tr ng-repeat="week in (days.length/7 | array)" ng-init="w = $index">
    <td ng-repeat="day in days.slice(7*$index, 7*$index + 7) track by $index" ng-init="i = w*7 + $index">
        {{ day }}
        <i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[i].correct"></i>
        <i class="icon ion-close answer-wrong" ng-if="submitted && !answers[i].correct"></i>
    </td>
 </tr>
a better oliver
  • 26,330
  • 2
  • 58
  • 66
0

Should be something like this:

<i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[$parent.$parent.$index * 7 + $parent.$index].correct"></i>

Basically, take the $parents index (the one where you create the week) multiply it by 7 and then add the other $index in. I suggest outputting that until you're sure it's right.

Mathew Berg
  • 28,625
  • 11
  • 69
  • 90