3

そのため、カスタム モジュールに表示するグリッドを取得しようとしています (当面は何でも表示しますが、機能したらコレクションについて心配します!)。

問題は、グリッド ウィジェット クラスの _prepareCollection() および/または _prepareColumns() メソッドが呼び出されないようで、グリッドが表示されない (ボタンとヘッダー テキストも表示されない) ことです。(Magento admin のヘッダーとフッター、およびナビゲーションが正しく表示されます。真ん中が空白になっているだけです!)

これは私がこれまでに持っているものです:

アプリ/コード/ローカル/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>

次に、私のコントローラー:

アプリ/コード/ローカル/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();
    }
}

次に、私のグリッドコンテナで:

アプリ/コード/ローカル/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();
    }
}

次に、私のグリッド ウィジェットで:

アプリ/コード/ローカル/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:

アプリ/デザイン/管理者html/デフォルト/デフォルト/レイアウト/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 である必要があります)。それらを変更しましたが、違いはありません

4

3 に答える 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

それはすべてあなたの選択です!

于 2012-08-24T15:33:56.177 に答える
0

index アクションが適切に呼び出されていることを確認してください。

これを行うとき:

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

}

于 2012-08-24T11:55:09.617 に答える