Php 是否将分页添加到Magento中的自定义模块?
我正在尝试在自定义模块中添加分页。我在articles.php的Block文件夹下有以下代码Php 是否将分页添加到Magento中的自定义模块?,php,magento,magento-1.5,Php,Magento,Magento 1.5,我正在尝试在自定义模块中添加分页。我在articles.php的Block文件夹下有以下代码 class Compname_Modname_Block_Articles extends Mage_Core_Block_Template { public function __construct() { parent::__construct(); $collection = Mage::getModel('articles/articles')-&
class Compname_Modname_Block_Articles extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$collection = Mage::getModel('articles/articles')->getCollection();
$this->setCollection($collection);
}
....
....
public function getTagsList(){
$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');
}
.........
..........
}
我有在管理员端cms页面有以下代码
<reference name="content">
<block type="articles/articles" name="articles.tags" as="tags.articles" template="articles/tags.phtml" />
</reference>
getSize()始终返回
NULL
,因此我无法获取集合值。请就此提供建议,您可以从Compname\u Modname\u block\u Articles::getTagsList()
这就是为什么
<?php $collection = $this->getTagsList();
var_dump($collection->getSize()); // Always return NULL
?>
True,自定义模块的解决方案
<?php
class Test_Featuredsalons_Block_Featuredsalons extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$collection = Mage::getModel('featuredsalons/featuredsalons')->getCollection();
$this->setCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
public function getCollection()
{
$limit = 10;
$curr_page = 1;
if(Mage::app()->getRequest()->getParam('p'))
{
$curr_page = Mage::app()->getRequest()->getParam('p');
}
//Calculate Offset
$offset = ($curr_page - 1) * $limit;
$collection = Mage::getModel('featuredsalons/featuredsalons')->getCollection()
->addFieldToFilter('status',1);
$collection->getSelect()->limit($limit,$offset);
return $collection;
}
}
?>
In phtml file, use below code:
<?php echo $this->getPagerHtml(); ?>
<?php $news = $this->getCollection(); ?>
在phtml文件中,使用以下代码:
谢谢,
卡西夫
<?php $collection = $this->getTagsList();
var_dump($collection->getSize()); // Always return NULL
?>
<?php
class Test_Featuredsalons_Block_Featuredsalons extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$collection = Mage::getModel('featuredsalons/featuredsalons')->getCollection();
$this->setCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setCollection($this->getCollection());
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
public function getCollection()
{
$limit = 10;
$curr_page = 1;
if(Mage::app()->getRequest()->getParam('p'))
{
$curr_page = Mage::app()->getRequest()->getParam('p');
}
//Calculate Offset
$offset = ($curr_page - 1) * $limit;
$collection = Mage::getModel('featuredsalons/featuredsalons')->getCollection()
->addFieldToFilter('status',1);
$collection->getSelect()->limit($limit,$offset);
return $collection;
}
}
?>
In phtml file, use below code:
<?php echo $this->getPagerHtml(); ?>
<?php $news = $this->getCollection(); ?>