2012-03-19 103 views
1

我想在自定義模塊中添加分頁。我有塊文件夾下,將下面的代碼articles.php將分頁添加到Magento中的自定義模塊中?

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

我有下面的代碼

<reference name="content"> 
      <block type="articles/articles" name="articles.tags" as="tags.articles" template="articles/tags.phtml" /> 
    </reference> 

在文章中我的主題文件夾CMS頁面管理員年底,我有文件名爲tags.phtml具有這樣的代碼,

<?php echo $this->getPagerHtml(); ?> // this displays exact pagination with page numbers 
    <?php $collection = $this->getTagsList(); 
    var_dump($collection->getSize()); // Always return NULL 
    ?> 

的getSize()總是返回NULL,所以我沒有得到我的收藏價值。在這個

回答

1

請諮詢您從Compname_Modname_Block_Articles::getTagsList()

public function getTagsList(){ 

    return $this;     
} 

回報您的塊類的實例這就是爲什麼,當然

<?php $collection = $this->getTagsList(); 
var_dump($collection->getSize()); // Always return NULL 
?> 
0

真,自定義模塊的解決方案。

<?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(); ?> 

感謝, 卡希夫

相關問題