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.

General Questions /

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

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

Pagination doesn't display, help plz!


Reply


4 Posts   1506 Views

Avatar
Mackodlak

Community Member, 95 Posts

24 May 2011 at 10:13pm

Edited: 24/05/2011 11:30pm

Hello, I am trying to use pagination with dataobjects, and it is not working properly.

The function to get Dataobjects looks like this:

function getLOTDs($pageLimit=5){

if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
$SQL_start = (int)$_GET['start'];

$getLOTDs = DataObject::get('LOTD',"","Created DESC","","");

$returnlotds = $getLOTDs->getRange($SQL_start, $pageLimit);

return $returnlotds;
}

the template to display them looks like this:

<% control getLOTDs %>
<li>
<a href="$LOTD">$LName</a>
</br>
<% if Top.canDelLOTD %>
<a href="LOTD/delLOTD/$ID">Brisanje</a>
<% end_if %>
<% if Top.canEditLOTD %>
<a href="$EditLOTDForm">Izmijeni</a>
<% end_if %>

</li>
<% end_control %>

<% include LOTDPagination %>

the LOTDPagination looks like this:

<% if getLOTDs.MoreThanOnePage %>
<div id="PageNumbers">
<p>
<% if getLOTDs.NotFirstPage %>
<a class="prev" href="$getLOTDs.PrevLink" title="View the previous page">Prev</a>
<% end_if %>

<span>
<% control getLOTDs.PaginationSummary(4) %>
<% if CurrentBool %>
$PageNum
<% else %>
<% if Link %>
<a href="$Link" title="View page number $PageNum">$PageNum</a>
<% else %>
&hellip;
<% end_if %>
<% end_if %>
<% end_control %>
</span>

<% if getLOTDs.NotLastPage %>
<a class="next" href="$getLOTDs.NextLink" title="View the next page">Next</a>
<% end_if %>
</p>
</div>
<% end_if %>

Can't seem to find an error, the output I get from this is:

Interni Carnet web
Brisanje Izmijeni
Monitor
Brisanje Izmijeni
YouTube
Brisanje Izmijeni
Index.hr
Brisanje Izmijeni
Net.hr
Brisanje Izmijeni

Default Admin
Last visit: 24 May 2011

Log Out

So it displays only the 5 links properly, but it doesn't display the available pages and pagination bar where it should. Can som1 plz help?

Avatar
martimiz

Forum Moderator, 1132 Posts

25 May 2011 at 3:55am

Edited: 25/05/2011 3:56am

Pagination will not work for DataObjectSets that are filtered afterwards - so first collecting the complete DataObjectSet and then returning a range of it will fail. Try using start, limit in your DataObject::get(). Something like this:

return DataObject::get(
   $callerClass = 'LOTD',
   $filter = "",
   $sort = 'Created DESC',
   $join = '',
   $limit = "$SQL_start, $pageLimit"
);

See also this article http://www.ssbits.com/tutorials/2010/paginating-a-filtered-dataobjectset/

Avatar
Ben_W

Community Member, 80 Posts

25 May 2011 at 12:43pm

The following uses a custom query to construct the DataObjectSets, then uses setPageLimits() to tap into the silverstripe pagenation, worked for me.

var $page_limit = 25;
function ListCategoryListing(){

if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) {
$SQL_start = 0;
} else {
$SQL_start = (int)$_GET['start'];
}

$count_query = "SELECT list.* FROM Listing list, SiteTree_Live st WHERE (list.CategoryID=$this->ID OR (st.ClassName='CategoryPage' AND st.ParentID=$this->ID) ) AND list.CategoryID=st.ID AND list.Active='1' ORDER BY list.BusinessName";
$page_query = $count_query." LIMIT {$SQL_start}, $this->page_limit";

if(DB::query($page_query)->valid()){
$records = DB::query($page_query);
$count = DB::query($count_query)->numRecords();

$myData = singleton('Listing')->buildDataObjectSet($records);
//maunally set page limit in a cruel way.
$myData->setPageLimits($SQL_start, $this->page_limit, $count);
} else {
$myData = false;
}

return $myData;
}

in the template.

<% if ListCategoryListing %>
<% if ListCategoryListing.MoreThanOnePage %>   
   <!-- Pager -->
   <div class="pagination">
      <span>Go to page:</span>
      
      <% if ListCategoryListing.PrevLink %>
         <a href="$ListCategoryListing.PrevLink" class="prev">Prev</a>
      <% else %>
         <span class="nolink">Prev</span>
      <% end_if %>
      
      <% control ListCategoryListing.PaginationSummary(4) %>
      <% if CurrentBool %>
         <span class="active">$PageNum</span>
      <% else %>
         <% if Link %>
            <a href="$Link" title="Go to page $PageNum">$PageNum</a>
         <% else %>
            ...
         <% end_if %>
      <% end_if %>
      <% end_control %>
      
      
      <% if ListCategoryListing.NextLink %>
         <a href="$ListCategoryListing.NextLink" class="next">Next</a>
      <% else %>
         <span class="nolink">Next</span>
      <% end_if %>
      
   </div>
   <!--// Pager -->
<% end_if %>
<% end_if %>

<% if ListCategoryListing %>
<% control ListCategoryListing %>
<!-- result -->
<div class="listing">
   <div class="title">$BusinessName</div>
   ...
   
   
</div>
<!-- /result -->
<% end_control %>
<% end_if %>

hope this helps.

Avatar
Mackodlak

Community Member, 95 Posts

25 May 2011 at 7:24pm

Martimiz thank you a lot, worked like a charm.
Ben, I am sorry, i didn't have time to try ur approach, cause I have loads of other stuff to do, and martimizes advice worked, but I appreciate the attempt, I will try it out sometimes.
Ty guys, this problem is solved!