2012-06-23 00:32:43 +02:00
|
|
|
# Grouping lists of records
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
The [api:SS_List] class is designed to return a flat list of records.
|
|
|
|
These lists can get quite long, and hard to present on a single list.
|
|
|
|
[Pagination](/howto/pagination) is one way to solve this problem,
|
|
|
|
by splitting up the list into multiple pages.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
In this howto, we present an alternative to pagination:
|
|
|
|
Grouping a list by various criteria, through the `[api:GroupedList]` class.
|
|
|
|
This class is a `[api:SS_ListDecorator]`, which means it wraps around a list,
|
|
|
|
adding new functionality.
|
|
|
|
|
|
|
|
It provides a `groupBy()` method, which takes a field name, and breaks up the managed list
|
|
|
|
into a number of arrays, where each array contains only objects with the same value of that field.
|
|
|
|
Similarly, the `GroupedBy()` method builds on this and returns the same data in a template-friendly format.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
## Grouping Sets By First Letter
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
This example deals with breaking up a [api:SS_List] into sub-headings by the first letter.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
Let's say you have a set of Module objects, each representing a SilverStripe module, and you want to output a list of
|
|
|
|
these in alphabetical order, with each letter as a heading; something like the following list:
|
|
|
|
|
|
|
|
* B
|
|
|
|
* Blog
|
|
|
|
* C
|
|
|
|
* CMS Workflow
|
|
|
|
* Custom Translations
|
|
|
|
* D
|
|
|
|
* Database Plumber
|
|
|
|
* ...
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
The first step is to set up the basic data model,
|
|
|
|
along with a method that returns the first letter of the title. This
|
2011-02-07 07:48:44 +01:00
|
|
|
will be used both for grouping and for the title in the template.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
class Module extends DataObject {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-06-23 00:32:43 +02:00
|
|
|
'Title' => 'Text'
|
2011-02-07 07:48:44 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first letter of the module title, used for grouping.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitleFirstLetter() {
|
|
|
|
return $this->Title[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
The next step is to create a method or variable that will contain/return all the objects,
|
|
|
|
sorted by title. For this example this will be a method on the `Page` class.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Page extends SiteTree {
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all modules, sorted by their title.
|
2012-06-23 00:32:43 +02:00
|
|
|
* @return GroupedList
|
2011-02-07 07:48:44 +01:00
|
|
|
*/
|
2012-06-23 00:32:43 +02:00
|
|
|
public function getGroupedModules() {
|
|
|
|
return GroupedList::create(Module::get()->sort('Title'));
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
The final step is to render this into a template. The `GroupedBy()` method breaks up the set into
|
|
|
|
a number of sets, grouped by the field that is passed as the parameter.
|
|
|
|
In this case, the `getTitleFirstLetter()` method defined earlier is used to break them up.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::ss
|
2012-06-23 00:32:43 +02:00
|
|
|
<%-- Modules list grouped by TitleFirstLetter --%>
|
2011-02-07 07:48:44 +01:00
|
|
|
<h2>Modules</h2>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $GroupedModules.GroupedBy(TitleFirstLetter) %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<h3>$TitleFirstLetter</h3>
|
|
|
|
<ul>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $Children %>
|
2011-02-07 07:48:44 +01:00
|
|
|
<li>$Title</li>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2011-02-07 07:48:44 +01:00
|
|
|
</ul>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
## Grouping Sets By Month
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
Grouping a set by month is a very similar process.
|
|
|
|
The only difference would be to sort the records by month name, and
|
|
|
|
then create a method on the DataObject that returns the month name,
|
|
|
|
and pass that to the [api:GroupedList->GroupedBy()] call.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
We're reusing our example `Module` object,
|
|
|
|
but grouping by its built-in `Created` property instead,
|
|
|
|
which is automatically set when the record is first written to the database.
|
|
|
|
This will have a method which returns the month it was posted in:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2012-06-23 00:32:43 +02:00
|
|
|
class Module extends DataObject {
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the month name this news item was posted in.
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-06-23 00:32:43 +02:00
|
|
|
public function getMonthCreated() {
|
|
|
|
return date('F', strtotime($this->Created));
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
The next step is to create a method that will return all records that exist,
|
|
|
|
sorted by month name from January to December. This can be accomplshed by sorting by the `Created` field:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
class Page extends SiteTree {
|
2012-06-23 00:32:43 +02:00
|
|
|
|
|
|
|
// ...
|
2012-10-02 11:38:16 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
/**
|
|
|
|
* Returns all news items, sorted by the month they were posted
|
2012-06-23 00:32:43 +02:00
|
|
|
* @return GroupedList
|
2011-02-07 07:48:44 +01:00
|
|
|
*/
|
2012-06-23 00:32:43 +02:00
|
|
|
public function getGroupedModulesByDate() {
|
|
|
|
return GroupedList::create(Module::get()->sort('Created'));
|
2011-02-07 07:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
The final step is the render this into the template using the [api:GroupedList->GroupedBy()] method.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::ss
|
2011-02-21 22:23:22 +01:00
|
|
|
// Modules list grouped by the Month Posted
|
2011-02-07 07:48:44 +01:00
|
|
|
<h2>Modules</h2>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $GroupedModulesByDate.GroupedBy(MonthCreated) %>
|
2012-06-23 00:32:43 +02:00
|
|
|
<h3>$MonthCreated</h3>
|
2011-02-07 07:48:44 +01:00
|
|
|
<ul>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $Children %>
|
2012-06-23 00:32:43 +02:00
|
|
|
<li>$Title ($Created.Nice)</li>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2011-02-07 07:48:44 +01:00
|
|
|
</ul>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2012-06-23 00:32:43 +02:00
|
|
|
|
|
|
|
## Related
|
|
|
|
|
|
|
|
* [Howto: "Pagination"](/howto/pagination)
|