2012-03-19 4 views
1

맞춤 모듈에 페이지 매김을 추가하려고합니다. 내가 기사에서Magento에서 사용자 정의 모듈에 페이지 매김을 추가 하시겠습니까?

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

내 테마 폴더에 다음 코드를 가진 관리자 결국 CMS 페이지를

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

articles.php에서 블록 폴더 아래에 다음 코드를 가지고, 내가 파일을 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(); ?> 

감사합니다, 제작 : Kashif

관련 문제