Question

I have an array of objects that have a "Category" property. In my ng-repeat, I am creating a group by conditionally showing a header row with the category name if it is different that the previous value by using items[$index - 1] to check the previous value. This works, but if I apply an orderBy filter, it would seem that $index doesn't reflect the new order. What am I doing wrong?

Was it helpful?

Solution

As soon as I posted this, I realized what might fix it. I added track by item.itemId and it started acting as expected.

<div class="row" ng-repeat="item in items track by item.itemId | filter: searchTerms | orderBy:['Category1', 'Category2']">
  <div class="rowHeader" ng-show="$index == 0 || item.Category1 != items[$index -1].Category1">
    {{item.Category1}}
  </div>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top