Magento: Paging in Custom Collection

Hello friends,

In last week I had a requirement to add paging facility in our custom module’s collection. Normally, magento provides paging in products collection. But this time, it is something different.

To satisfy that requirement, I had searched the web and found the best solution which I want to discuss here.

So now follow the under given steps:

Note: Here my module is Indies_Custom, so you have to change the class names according to your module.

Step 1: Controller (IndexController.php) file

In index controller simply load the layout and render it.

class Indies_Custom_IndexController extends Mage_Core_Controller_Front_Action
    public function indexAction()

Step 2: Layout (custom.xml) file

Put the under given code in the layout file of module.

<?xml version="1.0"?>
<layout version="0.1.0">
       <reference name="content">
           <block type="custom/collection" name="collection" template="custom/collection.phtml" />

Step 3: Block (Collection.php) file

Here we will create a ‘custom/collection’ block for the pager to set our collection in it.

class Indies_Custom_Block_Collection extends Mage_Core_Block_Template
    public function __construct()
        $collection = Mage::getModel('custom/collection')->getCollection();

    protected function _prepareLayout()

        $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
        $this->setChild('pager', $pager);
        return $this;

    public function getPagerHtml()
        return $this->getChildHtml('pager');

Step 4: Template (collection.phtml) file

At last, put the under given code in the template file of module.

Note: You can change the designing structure of this file according to your requirement.

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
    <h1><?php echo $this->__('My Custom Collection') ?></h1>
<?php echo $this->getPagerHtml(); ?>
<?php if($collection->getSize()): ?>
<table id="my-custom-table">
    <col width="1" />
    <col width="1" />
    <col />
    <col width="1" />
    <col width="1" />
    <col width="1" />
            <th><?php echo $this->__('ID #') ?></th>
            <th><?php echo $this->__('Title') ?></th>
            <th><span><?php echo $this->__('Created') ?></span></th>
        <?php $_odd = ''; ?>
        <?php foreach ($collection as $_obj): ?>
                <td><?php echo $_obj->getCollectionId() ?></td>
                <td><span><?php echo $_obj->getTitle(); ?></span></td>
                <td><?php echo $this->formatDate($_obj->getCreatedTime()) ?></td>
        <?php endforeach; ?>
<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 ?>

That’s it. Now you can see the paging in your custom collection page.

Hope this will help you.


Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: