Here is the following steps required to be followed -
Step1: Controller :
First in in our IndexController we will simply load the layout and render it.
<?php
class Test_Collection_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
Step2: Layout :
In our module layout file, i.e collection.xml this is the code to put
<?xml version="1.0"?>
<layout version="0.1.0">
<collection_index_index>
<reference name="content">
<block type="collection/collection" name="collection" template="collection/collection.phtml" />
</reference>
</collection_index_index>
</layout>
Step3: Block :
Next we will create our block ‘collection/collection’ where the main code for paging goes.
<?php
class Test_Collection_Block_Collection extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$collection = Mage::getModel('collection/collection')->getCollection();
$this->setCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setAvailableLimit(array(5=>5,10=>10,20=>20,'all'=>'all'));
$pager->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
As you can see in the above code we have created a block for the pager and set our collection in it.
Step4: phtml file :
Next in our collection.phtml file we need to put in this code
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<div class="page-title">
<h1><?php echo $this->__('My Collection') ?></h1>
</div>
<?php echo $this->getPagerHtml(); ?>
<?php if($collection->getSize()): ?>
<table class="data-table" id="my-custom-table">
<col width="1" />
<col width="1" />
<col />
<col width="1" />
<col width="1" />
<col width="1" />
<thead>
<tr>
<th><?php echo $this->__('ID #') ?></th>
<th><?php echo $this->__('Title') ?></th>
<th><span class="nobr"><?php echo $this->__('Created') ?></span></th>
</tr>
</thead>
<tbody>
<?php $_odd = ''; ?>
<?php foreach ($collection as $_obj): ?>
<tr>
<td><?php echo $_obj->getCollectionId() ?></td>
<td><span class="nobr"><?php echo $_obj->getTitle(); ?></span></td>
<td><?php echo $this->formatDate($_obj->getCreatedTime()) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('my-custom-table');</script>
<?php echo $this->getPagerHtml(); ?>
<?php else: ?>
<p><?php echo $this->__('The collection is empty.'); ?></p>
<?php endif ?>
This is all that is required, now the paging should show up on your collection.