Magento自定义模块网格未显示。

3

我正在尝试在我的自定义模块中显示网格(暂时显示任何内容,一旦它起作用,我会担心收集!)。

问题是我的网格小部件类的_prepareCollection()和/或_prepareColumns()方法似乎从未被调用,网格从未显示(按钮和标题文本也不显示)。 (Magento管理头和页脚和导航正确显示。只是中间是空白的!)

这是我到目前为止的进展:

app/code/local/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)。已更正,但没有任何改变。

3个回答

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>

为了解释和给出一些提示,您可以阅读以下内容:

我的Magento管理视图中的布局未加载

= 更新 =

您还不需要在控制器中调用$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();
}

请看定义:

Mage_Adminhtml_Controller_Action

protected function _addContent(Mage_Core_Block_Abstract $block)
{
    $this->getLayout()->getBlock('content')->append($block);
    return $this;
}

上述代码与mymodule.xml的作用相同,将块'mymodule/adminhtml_mymodule'追加到content中。

这都是您的选择!


就是这样!非常感谢 :) - sulman

0

请您确认一下是否正确调用了您的索引动作?

当您执行这个操作时:

<?php
class Mynamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
    echo "im here";exit; //<----does this display?
    $this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
    $this->loadLayout();
    $this->renderLayout();
}

}

最后,重要的是先加载您的布局。所以在我看来,将您的索引操作更改为:

<?php
class Mynamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
    $this->loadLayout();    // <---- This first
    $this->getLayout()->createBlock('mymodule/adminhtml_mymodule');// <---- then this
    $this->renderLayout();
}

}


indexAction() 正常工作(回显 ok),但更改加载顺序时无法正常运行。谢谢。 - sulman
你能确认一下你的网格构造函数是否被调用了吗? - activeDev
Mynamespace_Mymodule_Block_Adminhtml_Mymodule_Grid::__construct() 被调用了(实际上调用了两次!?)谢谢。 - sulman

0

我知道这与答案没有直接关系,但希望能对某些人有所帮助。

以防万一有人正在阅读此内容,完全失去了希望 - 确保在app/design/adminhtml/default/default/layout中有一个布局文件。

我在本地使用自定义网格没有任何问题,但当我迁移它时,它显示了上述症状(空白屏幕等)

这是因为我没有复制那个文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接