Jump to:

22979 Posts in 11815 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Pagination doesn't display, help plz!

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

Page: 1
Go to End
Author Topic: 1341 Views
  • Mackodlak
    Avatar
    Community Member
    95 Posts

    Pagination doesn't display, help plz! Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Pagination doesn't display, help plz! Link to this post

    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/

  • Ben_W
    Avatar
    Community Member
    79 Posts

    Re: Pagination doesn't display, help plz! Link to this post

    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.

  • Mackodlak
    Avatar
    Community Member
    95 Posts

    Re: Pagination doesn't display, help plz! Link to this post

    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!

    1341 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.