所以我试图在我的自定义模块中显示一个网格(暂时显示任何东西,一旦它工作我会担心这个集合!)。
问题是我的网格小部件类的 _prepareCollection() 和/或 _prepareColumns() 方法似乎从未被调用,并且网格从未显示(按钮和标题文本也不显示)。(Magento 管理员页眉和页脚以及导航正确显示。中间只是空白!)
这是我到目前为止所拥有的:
应用程序/代码/本地/MyNamespace/Mymodule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNamespace_Mymodule>
<version>0.0.1</version>
</MyNamespace_Mymodule>
</modules>
<!-- Define frontend and backend routers -->
<admin>
<routers>
<mymodule>
<use>admin</use>
<args>
<module>MyNamespace_Mymodule</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
</admin>
<!-- /Define frontend and backend routers -->
<global>
<helpers>
<mymodule>
<class>MyNamespace_Mymodule_Helper</class>
</mymodule>
</helpers>
<blocks>
<mymodule>
<class>MyNamespace_Mymodule_Block</class>
</mymodule>
</blocks>
</global>
<adminhtml>
<menu>
<mymodule module="mymodule">
<title>My Module</title>
<sort_order>80</sort_order>
<children>
<items module="mymodule">
<title>Manage My Module</title>
<sort_order>0</sort_order>
<action>mymodule/adminhtml_mymodule</action>
</items>
</children>
</mymodule>
</menu>
<!-- define layout updates -->
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
<!-- /define layout updates -->
</adminhtml>
</config>
然后我的控制器:
app/code/local/MyNamespace/Mymodule/controllers/Adminhtml/MymoduleController.php
<?php
class MyNamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->loadLayout();
$this->renderLayout();
}
}
然后在我的网格容器中:
app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule.php
<?php
class MyNamespace_Mymodule_Block_Adminhtml_Mymodule extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>";
parent::__construct();
$this->_controller = 'adminhtml_mymodule';
$this->_blockGroup = 'mymodule';
$this->_headerText = Mage::helper('mymodule')->__('my header text'); // this is not rendered
$this->_addButtonLabel = Mage::helper('mymodule')->__('my button text'); // this is not rendered
}
protected function _prepareLayout()
{
$this->setChild( 'grid',
$this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
$this->_controller . '.grid')->setSaveParametersInSession(true) );
return parent::_prepareLayout();
}
}
然后在我的网格小部件中:
app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule/Grid.php
<?php
class MyNamespace_Mymodule_Block_Adminhtml_Mymodule_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('mymoduleGrid');
$this->setDefaultSort('id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>"; // This is never called
$collection = Mage::getModel('catalog/product')->getCollection(); // just a temp collection for the time being
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>"; // This is never called
$this->addColumn('id', array(
'header' => Mage::helper('mymodule')->__('ID'),
'align' =>'right',
'width' => '10px',
'index' => 'id',
));
return parent::_prepareColumns();
}
}
最后是我的布局 xml:
app/design/adminhtml/default/default/layout/mymodule.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_mymodule_index>
<reference name="content">
<block type="mymodule/adminhtml_mymodule" name="mymodule" />
</reference>
</adminhtml_mymodule_index>
</layout>
日志中没有显示任何错误,我现在有点难过,其他 SO 答案似乎不合适。
任何人都可以解释为什么我的网格(即使是空的)没有显示?
谢谢你。
编辑注意到某些类的大小写错误(Mynamespace 应该是 MyNamespace)。改变了它们,但没有区别