所以我试图在我的自定义模块中显示一个网格(暂时显示任何内容,一旦它正在工作,我会担心它的集合!).
问题是我的网格小部件类的_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>
Run Code Online (Sandbox Code Playgroud)
然后我的控制器:
应用程序/代码/本地/ myNameSpace对象/ Mymodule中/控制器/ 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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的网格容器中:
应用程序/代码/本地/ myNameSpace对象/ Mymodule中/座/ 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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的网格小部件中:
应用程序/代码/本地/ myNameSpace对象/ Mymodule中/砌块/ 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();
}
}
Run Code Online (Sandbox Code Playgroud)
最后我的布局xml:
应用程序/设计/ adminhtml /默认/缺省/布局/ 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>
Run Code Online (Sandbox Code Playgroud)
日志中没有显示任何错误,我现在有点难过,其他SO答案似乎不合适.
有人说明为什么我的网格(即使是空的网格)没有显示?
谢谢.
编辑注意到某些类的错误情况(Mynamespace应该是MyNamespace).改变了它们但没有区别
iva*_*dja 10
这是布局的句柄标记的问题.
它应该是:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_adminhtml_mymodule_index>
<reference name="content">
<block type="mymodule/adminhtml_mymodule" name="mymodule" />
</reference>
</mymodule_adminhtml_mymodule_index>
</layout>
Run Code Online (Sandbox Code Playgroud)
有关解释和一些提示,您可以阅读:
= UPDATE =
您也不需要$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');按照您的控制方式调用控制器mymodule.xml
要么
您可以mymodule.xml通过将控制器的操作更改为:省略您的(无需调用它):
public function indexAction() {
$this->loadLayout();
$myblock = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->_addContent($myblock);
$this->renderLayout();
}
Run Code Online (Sandbox Code Playgroud)
看到定义:
Mage_Adminhtml_Controller_Action
protected function _addContent(Mage_Core_Block_Abstract $block)
{
$this->getLayout()->getBlock('content')->append($block);
return $this;
}
Run Code Online (Sandbox Code Playgroud)
以上这些代码做像一样的东西mymodule.xml,附加块'mymodule/adminhtml_mymodule'到content
这都是你的选择!