【问题标题】:Grid doesn't appear in custom admin module in Magento网格没有出现在 Magento 的自定义管理模块中
【发布时间】:2011-04-09 10:24:54
【问题描述】:

我正在尝试在 magento admin 中创建一个自定义模块。我已经到了将新链接添加到菜单中的地步,通过单击它,我可以导航到模块控制器的索引操作。但是这里我看不到网格,只有标题文本和块结构中添加的按钮出现。

我可以看到,由于这个块扩展了Mage_Adminhtml_Block_Widget_Grid_Container 类,它自己会在这个模块中添加网格块作为它的子模块。

并且包含了 Grid.php,我通过在覆盖的 _prepareColumns 方法中打印出一些内容来验证它。

我在这里缺少什么?

这些是 Grid.php 文件的内容

class Book_Brands_Block_Adminhtml_Brands_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    public function __construct() {
        parent::__construct();
        $this->setId('brandsGrid');
        $this->setDefaultSort('brands_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection() {       
        $collection = Mage::getModel('brands/brands')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns() {

        $this->addColumn('brands_id', array(
            'header' => Mage::helper('brands')->__('ID'),
            'align'  =>'right',
            'width'  => '50px',
            'index'  => 'brands_id',
        ));
        $this->addColumn('title', array(
            'header'=> Mage::helper('brands')->__('Title'),
            'align' =>'left',
            'index' => 'title',
        ));
        $this->addColumn('status', array(
            'header'=> Mage::helper('brands')->__('Status'),
            'align' => 'left',
            'width' => '80px',
            'index' => 'status',
            'type'  => 'options',
            'options' => array(
                1 => 'Enabled',
                2 => 'Disabled',
            ),
        ));
        $this->addColumn('action', array(
            'header' => Mage::helper('brands')->__('Action'),
            'width'  => '100',
            'type'   => 'action',
            'getter' => 'getId',
            'actions' => array(
                array(
                    'caption'  => Mage::helper('brands')->__('Edit'),
                    'url'  => array('base'=> '*/*/edit'),
                    'field' => 'id'
                )
            ),
            'filter'  => false,
            'sortable' => false,
            'index' => 'stores',
            'is_system' => true,
        ));
        return parent::_prepareColumns();
    }

    public function getRowUrl($row) {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
}

谢谢

PS。我试过刷新缓存但没有运气

【问题讨论】:

    标签: magento


    【解决方案1】:

    从记忆中我认为_prepareColumns() 是在_prepareCollection() 之前调用的,所以如果集合中有错误,即使您已经确认了 columns 方法,网格也不会被渲染。

    parent::_prepareCollection() 的一部分试图从集合的getSize()getSelectCountSql() 方法中估计页数,我经常忘记检查那些是否产生理智的结果,这让我很头疼。确保所有日志记录都已打开,并将以下内容放入您的 .htaccess 文件中:

    php_flag display_errors on
    SetEnv MAGE_IS_DEVELOPER_MODE true
    

    尝试使用这些命令查看正在生成的查询:

    Mage::log((string)$collection->getSelect());
    Mage::log((string)$collection->getSelectCountSql());
    

    【讨论】:

    • 是的,正如您所说,_prepareCollection 存在问题。将 display_errors 标志设置为 on 确实很有帮助。非常感谢!
    • @naiquevin 你在 _prepareCollection 中遇到了什么问题?我有类似的问题。在我的 Grid 类中, __construct 函数运行良好。但是,我的代码没有转到 _prepareCollection 函数。
    • @MukeshChapagain 我也遇到了同样的问题。你有解决办法吗?
    【解决方案2】:

    看起来您已经正确设置了网格块。但是,您仍然需要将网格加载到布局中并进行渲染。这可以在 adminhtml 布局 xml 或控制器中完成。

    在您的 /app/design/adminhtml/../layout/brands.xml 中:

    <?xml version="1.0"?>    
    <layout>
            <brands_index_index>
                <reference name="content">
                    <block type="brands/brands_grid" name="brands_grid"></block>
                </reference>
            </brands_index_index>
    </layout>
    

    在您的控制器中:

    public function indexAction()
    {
        $this->loadLayout();
        $this->_addContent(
            $this->getLayout()->createBlock('brands/brands_grid','brands')
        );
        $this->renderLayout();
    }
    

    请注意,您必须将上述内容修改为您的特定实现。我认为最初的布局 xml 比控制器中的程序化实例更难理解,但是从长远来看,它会减少代码膨胀。

    【讨论】:

      【解决方案3】:

      刚刚快速浏览了一下,我在你的代码中唯一能看到的是:

      受保护的功能_prepareCollection(){ $collection = Mage::getModel('brands/brands')->getCollection(); $this->setCollection($collection); 返回父级::_prepareCollection(); } //尝试像这样使用它: 受保护的功能_prepareCollection(){ $collection = Mage::getModel('brands/brands')->getCollection(); $this->setCollection($collection); 父::_prepareCollection(); 返回$这个; }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多