gpt4 book ai didi

Magento 自定义模块网格未显示

转载 作者:行者123 更新时间:2023-12-01 08:35:12 25 4
gpt4 key购买 nike

所以我试图在我的自定义模块中显示一个网格(暂时显示任何内容,一旦它工作,我会担心集合!)。

问题是我的网格小部件类的 _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>

然后是我的 Controller :

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 答案似乎不合适。

有人解释了为什么我的网格(即使是空的)没有显示?

谢谢。

EDIT 注意到某些类的大小写错误(Mynamespace 应该是 MyNamespace)。改变了它们,但没有区别

最佳答案

这是你布局的句柄标签的问题。

应该是:

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

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

My layout isn't loading in my Magento admin view

= 更新 =

你也不需要在你的 Controller 中调用 $this->getLayout()->createBlock('mymodule/adminhtml_mymodule'); 就像在你的 mymodule.xml 中一样

你可以省略你的 mymodule.xml (无需调用它),将 Controller 的操作更改为:

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 做同样的事情,将 block 'mymodule/adminhtml_mymodule' 附加到 content

这一切都是你的选择!

关于Magento 自定义模块网格未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12108697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com