SuperGroupedList
, (*1)
An extension of SilverStripeās GroupedList that supports traversing relations., (*2)
Note: depending on your data, the same items may be output multiple times. For example if a product belongs to multiple categories, and you group by category title, then the product will show under each of the categories that it belongs to., (*3)
Installation
composer require bigfork/supergroupedlist ^1.0
Or download and extract to a folder named supergroupedlist
in your document root., (*4)
Usage
Use exactly as you would use GroupedList
, but with dot-notation to traverse relations:, (*5)
public function GroupedProducts() {
$products = Product::get();
return SuperGroupedList::create($products);
}
<% loop $GroupedProducts.GroupedBy('Categories.Title') %>
<h1>{$Title}</h1><!-- Category title -->
<ul>
<% loop $Children %>
<li>{$Title}</li><!-- Product title -->
<% end_loop %>
</ul>
<% end_loop %>
You can traverse has_one
, has_many
and many_many
relations using dot notation. The last part of the notation you provide (Title
in the example above) will be both the field thatās extracted from the final component, and the $Variable
used to access that field inside the loop., (*6)
You can even traverse multiple relations at once. For example, $GroupedProducts.GroupedBy('Manufacturer.Employees.FavouriteTeam.Name')
would return a list of products grouped by the names of the favourite teams of the employees of the productās manufacturer., (*7)