0

I want to add pagination in custom data collection Magento 1.x. I found it and tried but it not working.

My table

enter image description here

My collection

public function getProductReturn(){
         $collection = Mage::getModel('catalog/product')->getCollection()
          ->addAttributeToSelect('sku');
          $collection->addFieldToFilter(array(
                    array('attribute'=>'return_item','eq'=>'1')
                ));
                $collection->setPage(2, 6);
          return $collection;
   }
0

->setPageSize(10) ->setCurPage(1);

Try above. You will get first 10 products. If you set CurPage to 2, you will get next 10 products. To understand simply, setting 2 returns 11 to 20 result.

0

You can use this code for pagination

In your Block file

    public function __construct()
    {
        parent::__construct();
        $collection = Mage::getModel('custom/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');
    }

In your phtml file add this line at last <?php echo $this->getPagerHtml(); ?>

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<div>
    <h1><?php echo $this->__('My Custom Collection') ?></h1>
</div>
<?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" />
    <thead>
        <tr>
            <th><?php echo $this->__('ID #') ?></th>
            <th><?php echo $this->__('Title') ?></th>
            <th><span><?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><?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 ?>

Reference

Hope it Helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.