2011-04-09 49 views
5

我正嘗試在magento admin中創建一個自定義模塊。我已經達到了一個新的鏈接添加到菜單,並點擊它,我可以導航到該模塊的控制器的索引行動。但是在這裏我看不到網格,只有標題文本和塊結構中添加的按鈕出現。網格不會出現在Magento中的自定義管理模塊中

我可以看到,由於該塊擴展了Mage_Adminhtml_Block_Widget_Grid_Container類,它本身會將此模塊內部的網格塊添加爲其子級。

而且包含了Grid.php,我通過在覆蓋範圍012b方法中打印出某些東西來驗證。

我在這裏錯過了什麼?

這些都是Grid.php文件的內容

class Book_Brands_Block_Adminhtml_Brands_Grid extends Mage_Adminhtml_Block_Widget_Grid { 

    public function __construct() { 
     parent::__construct(); 
     $this->setId('brandsGrid'); 
     $this->setDefaultSort('brands_id'); 
     $this->setDefaultDir('ASC'); 
     $this->setSaveParametersInSession(true); 
    } 

    protected function _prepareCollection() {  
     $collection = Mage::getModel('brands/brands')->getCollection(); 
     $this->setCollection($collection); 
     return parent::_prepareCollection(); 
    } 

    protected function _prepareColumns() { 

     $this->addColumn('brands_id', array(
      'header' => Mage::helper('brands')->__('ID'), 
      'align' =>'right', 
      'width' => '50px', 
      'index' => 'brands_id', 
     )); 
     $this->addColumn('title', array(
      'header'=> Mage::helper('brands')->__('Title'), 
      'align' =>'left', 
      'index' => 'title', 
     )); 
     $this->addColumn('status', array(
      'header'=> Mage::helper('brands')->__('Status'), 
      'align' => 'left', 
      'width' => '80px', 
      'index' => 'status', 
      'type' => 'options', 
      'options' => array(
       1 => 'Enabled', 
       2 => 'Disabled', 
      ), 
     )); 
     $this->addColumn('action', array(
      'header' => Mage::helper('brands')->__('Action'), 
      'width' => '100', 
      'type' => 'action', 
      'getter' => 'getId', 
      'actions' => array(
       array(
        'caption' => Mage::helper('brands')->__('Edit'), 
        'url' => array('base'=> '*/*/edit'), 
        'field' => 'id' 
       ) 
      ), 
      'filter' => false, 
      'sortable' => false, 
      'index' => 'stores', 
      'is_system' => true, 
     )); 
     return parent::_prepareColumns(); 
    } 

    public function getRowUrl($row) { 
     return $this->getUrl('*/*/edit', array('id' => $row->getId())); 
    } 
} 

感謝

PS。我已經嘗試刷新緩存但沒有運氣

回答

1

從內存中我認爲_prepareColumns()之前調用_prepareCollection()因此,如果集合中有錯誤,即使您已確認列方法,網格將不會呈現。

parent::_prepareCollection()的一部分嘗試估計來自集合的getSize()getSelectCountSql()方法的頁面數,我經常忘記檢查這些方法是否會產生令我感到不安的結果。確保所有日誌處於打開狀態,並在您.htaccess文件中的以下內容:

php_flag display_errors on 
SetEnv MAGE_IS_DEVELOPER_MODE true 

嘗試看到的是正在使用這些命令生成的內容的查詢:

Mage::log((string)$collection->getSelect()); 
Mage::log((string)$collection->getSelectCountSql()); 
+0

是有你說_prepareCollection問題。將display_errors標誌設置爲真有幫助。萬分感謝! – naiquevin 2011-04-12 06:04:31

+0

@naiquevin你在_prepareCollection中有什麼問題?我有類似的問題。在我的Grid類中,__construct函數工作正常。但是,我的代碼不會去_prepareCollection函數。 – 2011-10-05 04:08:46

1

看起來你有網格塊設置正確。但是,您仍然需要將網格加載到佈局中並進行渲染。這可以在adminhtml佈局xml或控制器中完成。

在你/app/design/adminhtml/../layout/brands.xml:

<?xml version="1.0"?>  
<layout> 
     <brands_index_index> 
      <reference name="content"> 
       <block type="brands/brands_grid" name="brands_grid"></block> 
      </reference> 
     </brands_index_index> 
</layout> 

在你的控制器:

public function indexAction() 
{ 
    $this->loadLayout(); 
    $this->_addContent(
     $this->getLayout()->createBlock('brands/brands_grid','brands') 
    ); 
    $this->renderLayout(); 
} 

請注意,您必須修改以上你的特定實現。我認爲佈局xml最初比控制器中的編程實例更難理解,但從長遠來看,它會導致更少的代碼膨脹。

0

剛剛有了一個快速瀏覽,我可以在你的代碼中看到的唯一的事情是:

 
protected function _prepareCollection() {  
     $collection = Mage::getModel('brands/brands')->getCollection(); 
     $this->setCollection($collection); 
     return parent::_prepareCollection(); 
    } 

//Try to use it like this: 
protected function _prepareCollection() {  
     $collection = Mage::getModel('brands/brands')->getCollection(); 
     $this->setCollection($collection); 
     parent::_prepareCollection(); 
     return $this; 
    } 
相關問題