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.

Template Questions /

Pagination problem when using children


Reply


2 Posts   507 Views

Avatar
ShadeFrozen

Community Member, 10 Posts

27 June 2013 at 12:50pm

Hi,

It's been a while since I've worked with SilverStripe (due to employment and time constraints) but now I'm jumping back in as it is something I truly enjoy. I started working with an old install running SilverStripe 2.4 and all is going well (will upgrade to 3.0 in the next project but that will be next week). One issue I am struggling to resolve is pagination on children. I have the search function working and pagination on a query works fine. But I am unsure of how to implement it using the following code:

AccommodationPage.php
---------------------------

<?php

class AccommodationPage extends Page {

//db fields
static $db = array (
'Name' => 'Varchar(255)',
'Website' => 'Varchar(100)',
'Email' => 'Varchar(100)'
);

static $has_one = array(
'Photo' => 'Image'
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Images', new ImageField('Photo'));
$fields->addFieldToTab('Root.Content.Main', new TextField('Name'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('Website'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new EmailField('Email'), 'Content');
return $fields;
}

}

class AccommodationPage_Controller extends Page_Controller {

//tried adding the below and using a control on AccommodationPageList but this outputs nothing
public function AccommodationPageList($limit=10){
$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
$list = DataObject::get("AccommodationPage", "Website", "$start", "$limit");   
return $list;
}

}
?>

AccommodationHolder.php
---------------------------
<?php

class AccommodationHolder extends Page
{
static $db = array(
);
static $has_one = array(
);

static $allowed_children = array('AccommodationPage');

}

class AccommodationHolder_Controller extends Page_Controller {

}

?>

AccommodationPage.ss
-----------------------

<div id="main">
   <% include Breadcrumbs %>
   $Content
</div>

<div id="sidebar">
   <% include Navigation2 %>
</div>   

AccommodationHolder.ss
------------------------------

<div id="accommodation">

<% include Breadcrumbs %>
$Content

   
<% control Children %>

<div class="accommodation-wrapper">
<div class="header-summary">
<h4 style="display:inline">$Name</h4>
<span class="website">
<a href="$Website">Website</a>
</span>
<span class="email">
<a href="mailto: $Email">Email</a>
</span>
</div>

<div class="photo">$Photo.SetWidth(150)</div>
<div class="description">
$Content
</div>
</div>
      
<% end_control %>

</div>

<br />

<% control AccommodationPageList %>
<p>$Website</p>
<% end_control %>

******************************

So any ideas on how to get pagination on the accommodation list? I think I need to do a separate query and simply call that in the control.

TIA and all help appreciated.

//Mark

Avatar
ShadeFrozen

Community Member, 10 Posts

27 June 2013 at 1:59pm

OK so after a little more googling , reading the docs and rebuilding the db, I have the basic concept functioning.
Added the below to AccommodationHolder:

function AccommodationListings($pagination=true) {
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
$Limit = $pagination==true ? (int)$_GET['start'].",1" : null;

$AccommodationListings = DataObject::get(
'AccommodationPage',
null,
'Name DESC',
null,
$Limit
);
return !empty($AccommodationListings) ? $AccommodationListings : false;
}

... and it works in the template with the standard controls. Now to implement a customised pagination (plenty of examples on that one)!!