I have an angularJS controller with a list of Events. Every event has Date and Comment. I'm displaying a list of events and want to hide the same dates.
For instance if I have:
{Date: "10/1/2013", Comment= "First Event"},
{Date: "10/1/2013", Comment= "Second Event"},
{Date: "10/2/2013", Comment= "Third Event"}
The output should be:
10/1/2013 Tuesday
First Event
Second Event
10/2/2013 Wednesday
Third Event
Here is my template:
<div data-ng-repeat="item in Events | filter:Filter | orderBy:SortBy:SortDesc">
<div data-ng-show="item.DisplayDate($index)" class="eventDate">{{ item.Date | date: 'M/dd/yyyy EEEE' }}</div>
<div class="comment" ng-bind-html-unsafe="item.Comment"></div>
<div style="clear:both"><br /></div>
</div>
I added a function to each Event item which compares event dates and if previous event has the same date returns false (hides Event Date div).
$scope.DisplayDate = function (idx) {
if (idx > 0) {
//get previous event
var previousEvt = $scope.Events[idx - 1];
//if previous event has the same date as this one - don't show subheader
if (previousEvt.Date.getTime() == $scope.Events[idx].Date.getTime())
return false;
else
return true;
}
else
return true;
}
It works great if we do not apply filter or order to the Events array. If order/filter is applied - DisplayDate method is evaluated on original Events array (which is incorrect).
The questions are:
- Is it possible to get access to filtered/ordered Events array in the DisplayDate method?
- Are there any other ways to show/hide group headers? I think I can apply some Date groupping and have two nested ng-repeats but it looks more complicated.
Thanks,