There's one of my friends asking how to create paging in query results on silverstripe?
It is to display the query results we must make the results is limited per page. Paging is the term used to regulate the number of query results are displayed per page. If not using the paging all the query results will be displayed on one page and it is not reasonable if it is implemented on the website. We can imagine, if the google.com search results do not display in the form of paging, then we must make scrolling to find the search results to the bottom of result page.
In fact Silverstripe paging facilities already provided by the DataObjectSet object. This object is used to store a collection of DataObject. DataObjek is a Class to represent the Silverstripe Database. You can read how to use DataObject in http://doc.silverstripe.org/doku.php?id=dataobject.
Create paging on silverstripe actually easy. We class to use Class DataObjectSet on our query function. Suppose we make a function ArticlePageList whose content type to display the list of pages that we will restrict ArticlePage 10 per page, its code as follows:
public function ArticlePageList($limit=10){
$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
$list = DataObject::get("ArticlePage", "", "","","$start , $limit");
return $list;
}
In functions there is a $list variable which is the DataObjectSet output of the static method DataObject: get. In this function we also determine the limit of the number of query results. Then on the template we can create the following code to display it:
<ul>
<% control ArticlePageList %>
<li><a href="$Link">$Title</a><p>$Content.Summary(50)</p></li>
<% end_control %>
</ul>
Above code wil be shown a list of type list of pages ArticlePage limit to 10 ArticlePage per Page.
To make the paging, we just use the method that has been provided by DataObjectSet. Method which we can use for paging is as follows:
- MoreThanOnePage(), this method is used to determine if the output of the query results more than one page or not.
- NextLink(), this method is used to get the URL for the next page.
- NotFirstPage(), this method is used to provide information that the query results are displayed not on the first page.
- NotLastPage(), this method is used to provide information that the query results are displayed not on the last page.
- PrevLink(), this method is used to obtain the URL to the previous page.
to use the methods, we can directly implements it on the template silverstripe as below:
<% if LatestChild.NotFirstPage %>
<a class="prev" href="$LatestChild.PrevLink">« Sebelumnya</a>
<% end_if %>
<% if LatestChild.NotLastPage %>
<a class="next" href="$LatestChild.NextLink">Berikutnya »</a>
<% end_if %>
In the above code there will be displayed a link to the previous page and next page for the ArticlePage list.