magento 对自定义集合进行分页

标签 magento

我有一个自定义页面,我在其中使用自定义查询通过针对品牌交叉模型来显示自定义产品。我有一系列产品 ID,现在我似乎无法弄清楚如何实现 magento 的默认分页系统。

任何帮助将不胜感激。

提前致谢

最佳答案

第 1 步: Controller (IndexController.php) 文件 在索引 Controller 中,只需加载布局并渲染它。

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

第 2 步:布局 (custom.xml) 文件 将下面给出的代码放入模块的布局文件中。

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

这是 block 的代码,您可以在其中定义集合

<?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');
    }
}

您必须定义自定义集合,它根据您的要求返回产品 ID。在上面的代码中 这是 phtml 文件,您可以在其中获取分页

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

希望这对你有帮助

关于magento 对自定义集合进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18664130/

相关文章:

通过阅读教程了解 Magento 基本问题

javascript - 如何减少我的 magento 网站弹出窗口中的 js 和 css 加载时间

c# - 带有警告和 InvalidOperationException 的 WCF WebService

php - 跟踪像素 - PHP 问题

javascript - Uncaught ReferenceError : jQuery is not defined in magento site

php - Amazon EC2 上的 Magento

Magento 更改单页结账页面标题

Magento 模块在管理中上传图像

magento - 在 magento 下订单时通过电子邮件通知管理员

php - Magento如何创建动态功能?