Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Nested Grouped Lists


Reply

3 Posts   463 Views

Avatar
cumquat

13 July 2013 at 2:12am Community Member, 198 Posts

Hi there,

I'm trying to create a grouped list with two groupings, im using 3.1 and i have it working lovely with just one grouping but for the life of me i cant figure out how to make it two groupings.
The table has a number of columns one called stage the other called category and i'd like to list all records by stage then within each stage by category.

below is my code from the dataobject and on the template.

hope you can help.

Mick

<div class="checklist">
                  <% loop groupedChecks.GroupedBy(Stage) %>
                  <div class="row ">
                     <div class="large-12 columns field">
                        <h5>$Stage</h5>
                     </div>
                  </div>
                     <% loop Children %>
                        <div class="row">
                           <div class="large-12 columns ">
                              <div class="large-9 columns field">
                                 <p>$Name</p>
                              </div>
                              <% if Checked %>
                                  <div class="large-3 columns field">
                                    <div class="roundedOne">
                                       <input type="checkbox" value="0" id="roundedOne $ID" name="$ID" checked="checked" />
                                       <label for="roundedOne $ID"></label>
                                    </div>
                                 </div>
                              <% else %>
                                 <div class="large-3 columns field">
                                    <div class="roundedOne">
                                       <input type="checkbox" value="1" id="roundedOne $ID" name="$ID" />
                                       <label for="roundedOne $ID"></label>
                                    </div>
                                 </div>
                              <% end_if %>
                           </div>
                        </div>
                     <% end_loop %>
                  <% end_loop %>
               </div>


function groupedChecks() {
return GroupedList::create(TheCheck::get()->filter('ProjectID' , $this->ID));
}

Avatar
cumquat

15 July 2013 at 11:33pm (Last edited: 16 July 2013 1:49am), Community Member, 198 Posts

**bump** anyone?

Im sure in 2.4 you could also then group on the child loop so in this case it would be.
as in this example http://www.silverstripe.org/general-questions/show/9070

<% loop groupedChecks.GroupedBy(Stage) %>
   <div class="row ">
   <div class="large-12 columns field">
      <h5>$Stage</h5>
   </div>
   </div>
      <% loop Children.GroupedBy(Category) %>
<h5>$Category</h5>
         <% loop Children %>
<div class="row">
            <div class="large-12 columns ">
               <div class="large-9 columns field">
                  <p>$Name</p>
               </div>
</div>
</div>
<% end_loop %>
<% end_loop %>
<% end_loop %>

Avatar
RobboDev

20 February 2014 at 5:50pm (Last edited: 20 February 2014 5:51pm), Community Member, 21 Posts

Hey Bro,

Just done this. See attached file "NestedGroupedList" only allows 2 levels of nesting, cos i couldn't be arsed to do "infinite" levels.

With it you can group by 2 different aspects of the data object, first return the list in your function:

public function GroupedEntries() {
return NestedGroupedList::create(
   BlogEntry::get()->sort('Year DESC, Month ASC')
);
}

Then in your template, separate the 2 different groupings with a comma:

<% loop $GroupedEntries.GroupedBy('Year,Month') %>
       <h3>$Year</h3>
       <ul>
       <% loop $Children %>
       <h3>$Month</h3>
       <ul>
             <% loop $Children %>
             <li>$Title</li>
             <% end_loop %>
             </ul>
       <% end_loop %>
       </ul>
      <% end_loop %>

NB: this wont work for a default blog entry unless you extended yours with the year and month fields.

Chur BOI!

Attached Files