Magento自定义模块网格不显示 [英] Magento custom module grid not displaying

查看:59
本文介绍了Magento自定义模块网格不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图在我的自定义模块中显示一个网格(暂时不显示任何内容,我担心集合一旦起作用!).

So I'm trying to get a grid to display in my custom module (displaying anything for the time being, I'll worry about the collection once it's working!).

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

The problem is that the _prepareCollection() and/or _prepareColumns() methods of my grid widget class never seem to get called and the grid never shows (nor do the buttons and header text). (The Magento admin header and footer and navigation display correctly. It's just blank in the middle!)

这是我到目前为止所拥有的:

This is what I have so far:

app/code/local/MyNamespace/Mymodule/etc/config.xml

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

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();
    }
}

然后在我的网格容器中:

Then in my grid container:

app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule.php

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();
    }
}

然后在我的网格小部件中:

Then in my grid widget:

app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule/Grid.php

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:

And finally my layout xml:

app/design/adminhtml/default/default/layout/mymodule.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答案似乎不合适.

There are no errors being shown in the logs and I'm now a bit stumpped and other SO answers don't seem to fit.

有人对我的网格(甚至是一个空的网格)为什么不显示有何了解?

Anyone shed any light on why my grid (even an empty one) isn't showing?

谢谢.

编辑注意,某些类的大小写错误(Mynamespace应该为MyNamespace).改变了但没有区别

EDIT Noticed that some classes had the wrong case (Mynamespace should be MyNamespace). Changed them but no difference

推荐答案

这是布局的handle标签的问题.

It is the problem of your layout's handle tag.

应该是:

<?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>

有关说明和一些提示,您可以阅读以下内容:

For the explanation and few tips, you can read this:

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

=更新=

您也不必像在mymodule.xml

OR

您可以通过将控制器的操作更改为以下内容来省略mymodule.xml(无需调用它):

you can omit your mymodule.xml (no need to call it) by changing your controller's action into:

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

those codes above do the same thing like the mymodule.xml, appending block 'mymodule/adminhtml_mymodule' to the content

这都是您的选择!

这篇关于Magento自定义模块网格不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆