magento对自定义集合进行分页 [英] magento paginate a custom collection
问题描述
我有一个自定义页面,其中通过将模型与品牌交叉来使用自定义查询显示了自定义产品.我现在有一系列产品ID,但似乎无法弄清楚如何实现magento的默认分页系统.
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 figure out is how to implement the magento's default paging system.
任何帮助将不胜感激.
预先感谢
推荐答案
步骤1:控制器(IndexController.php)文件 在索引控制器中,只需加载布局并呈现它即可.
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();
}
}
?>
第2步:布局(custom.xml)文件 将下面给出的代码放入模块的布局文件中.
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');
}
}
您必须定义自定义集合,该集合将根据您的要求返回产品ID.在上述代码中 这是您可以在其中分页的phtml文件
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 ?>
希望这对您有帮助
这篇关于magento对自定义集合进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!