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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Nested Grouped Lists


Go to End


3 Posts   2660 Views

Avatar
cumquat

Community Member, 201 Posts

13 July 2013 at 2:12am

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

Community Member, 201 Posts

15 July 2013 at 11:33pm

Edited: 16/07/2013 1:49am

**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
Rob Clarkson

Community Member, 26 Posts

20 February 2014 at 5:50pm

Edited: 20/02/2014 5:51pm

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