Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJs show/hide subheaders in ng-repeat
    primarykey
    data
    text
    <p>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.</p> <p>For instance if I have:</p> <pre><code>{Date: "10/1/2013", Comment= "First Event"}, {Date: "10/1/2013", Comment= "Second Event"}, {Date: "10/2/2013", Comment= "Third Event"} </code></pre> <p>The output should be:</p> <pre><code> 10/1/2013 Tuesday First Event Second Event 10/2/2013 Wednesday Third Event </code></pre> <p>Here is my template: </p> <pre><code>&lt;div data-ng-repeat="item in Events | filter:Filter | orderBy:SortBy:SortDesc"&gt; &lt;div data-ng-show="item.DisplayDate($index)" class="eventDate"&gt;{{ item.Date | date: 'M/dd/yyyy EEEE' }}&lt;/div&gt; &lt;div class="comment" ng-bind-html-unsafe="item.Comment"&gt;&lt;/div&gt; &lt;div style="clear:both"&gt;&lt;br /&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>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).</p> <pre><code> $scope.DisplayDate = function (idx) { if (idx &gt; 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; } </code></pre> <p>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).</p> <p>The questions are:</p> <ol> <li>Is it possible to get access to filtered/ordered Events array in the DisplayDate method? </li> <li>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.</li> </ol> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload