1

I have this array of objects (left side) and I want to index it like this (right side) using ng-repeat.

 i   j        |  x
[             |     
 0            |  0   x = i
 1            |  1
 2            |  2
 3 [          |  
     0        |  3   x = i + j
     1        |  4
     2        |  5
   ]          |  
 4            |  6   x = ?
 5 [          |  
     0        |  7   x = ?
     1        |  8
     2        |  9
   ]          |
]             |

It kinda works with using $parent.$index+$index in the nested array, but this works only in the first nested array as the next ones will have their $index start at 0 again so 5+0 = 5, not 7. Also, the indexing of the main array will not count for the nested array elements after.

What I actually need is a counter which I can display in a {{}} block.

<div ng-repeat-start="item in array track by $index">
   {{$index}}
</div>
<div ng-if="isArray(item)" ng-repeat="subItem in item track by $index">
   {{$parent.$index+$index}}
</div>
<hr ng-repeat-stop>

I was thinking that maybe storing "x" as a member in each item from the array could work, but then every time I would remove one item from the array I will have to process the array again to fix the "x".

edit: My array is actually the content of a shopping cart. Each item in the array is a Product, but some of these items can be a Package that includes more products along a discount.

4
  • 1
    Do you want to flatten you array? Commented Jan 13, 2016 at 11:50
  • I can't flatten the array because it contains objects with many members and some of those object's members are an array of objects. Commented Jan 13, 2016 at 12:40
  • 1
    @Adi.S Actually you are wrong about this: "but then every time I would remove one item from the array I will have to process the array again to fix the "x"". Look in this short example: plnkr.co/edit/87T7jQAMHJq1OE33ZFYx?p=preview Commented Jan 15, 2016 at 9:48
  • @diana-r I already saw that kind of implementation, but that wouldn't work for me in this case because not all of my items are nested arrays and those that aren't have to be counted too. You could say that I could add those as a single entry array, but that would be just the reverse of flattening the main array. Commented Jan 15, 2016 at 11:46

1 Answer 1

1

After a bit of rethinking the entire problem I came to the conclusion that indexing this nested array using ng-repeats complicates the page too much and by flattening the array I would solve more problems than I would create.

TL:DR I've flattened the array and by doing this I've simplified many more pages and only had to add a few new functions to deal with the packages' discounts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.