Reputation: 291
How do a I create a paginated menu list of children (i.e. Menu(2)
). I tried
$list = Menu::get();
but class Menu does not exist. Is it best to iterate Menu(2) and assign it to a DataArray? I also tried
$list = Page::get();
but it doesn't even show any pages?
Upvotes: 0
Views: 230
Reputation: 15794
In SilverStripe 3.1 we can call $this->getMenu(2)
in our controller to get the navigation menu of the given level (level 2 in this case).
We can then use PaginatedList
to turn that menu into a paginated list. There are some great resources on how to create a paginated list such as:
We can use these to create a PaginatedMenu function that will return a paginated list of our menu items:
class Page_Controller extends ContentController {
public function PaginatedMenu($level = 1) {
$paginatedMenu = PaginatedList::create(
$this->getMenu($level),
$this->request
);
$paginatedMenu->setPageLength(5);
$paginatedMenu->setPaginationGetVar('menu-start');
return $paginatedMenu;
}
}
setPageLength
allows us to set the number of items to display per page.
Here is an example of how to use this in our template:
<% if $PaginatedMenu(2) %>
<ul class="paginatedMenu">
<% loop $PaginatedMenu(2) %>
<li class="$LinkingMode"><a href="$Link">$MenuTitle</a></li>
<% end_loop %>
</ul>
<% if $PaginatedMenu(2).MoreThanOnePage %>
<div class="pagination">
<% if $PaginatedMenu(2).NotFirstPage %>
<a href="$PaginatedMenu(2).PrevLink" class="prev" aria-label="View the previous page">←</a>
<% end_if %>
<span>
<% loop $PaginatedMenu(2).PaginationSummary %>
<% if $CurrentBool %>
$PageNum
<% else_if $PageNum %>
<a href="$Link" class="pageLink" aria-label="View page number $PageNum">$PageNum</a>
<% end_if %>
<% end_loop %>
</span>
<% if $PaginatedMenu(2).NotLastPage %>
<a href="$PaginatedMenu(2).NextLink" class="next" aria-label="View the next page">→</a>
<% end_if %>
</div>
<p>Page $PaginatedMenu(2).CurrentPage of $PaginatedMenu(2).TotalPages</p>
<% end_if %>
<% end_if %>
Upvotes: 2