magento paginate a custom collection

前端 未结 1 436
粉色の甜心
粉色の甜心 2021-01-22 08:16

I have a custom page where I have displayed custom products using custom queries by crossing models against brands. I have an array of products Ids now what I can\'t seem to fig

相关标签:
1条回答
  • 2021-01-22 08:53

    Step 1: Controller (IndexController.php) file In index controller simply load the layout and render it.

    <?php
    class Abc_Example_IndexController extends Mage_Core_Controller_Front_Action
    {
        public function indexAction()
        {
            $this->loadLayout();
            $this->renderLayout();
        }
    }
    ?>
    

    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">
       <example_index_index>
           <reference name="content">
               <block type="example/collection" name="collection" template="example/collection.phtml" />
           </reference>
       </example_index_index>
    </layout>
    

    here is code for block where you can define collection

    <?php
    class Abc_Example_Block_Collection extends Mage_Core_Block_Template
    {
        public function __construct()
        {
            parent::__construct();
            $collection = Mage::getModel('example/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');
        }
    }
    

    you have to define custom collection which return the product id as per your requirement.in above code here is the phtml file where you can get pagination

    <?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 ?>
    

    hope this will help you

    0 讨论(0)
提交回复
热议问题