网格不会出现在 Magento 的自定义管理模块中

发布于 2024-10-31 11:13:32 字数 2641 浏览 1 评论 0原文

我正在尝试在 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。我尝试过刷新缓存但没有成功

I am trying to create a custom module in magento admin. I have reached the point where a new link has been added to the menu and by clicking on it, I can navigate to the index action of the controller of the module. But here I cannot see the grid, only the header text and the button which has been added in the block construct appear.

I can see that since this block extends the Mage_Adminhtml_Block_Widget_Grid_Container class, it will by itself add the grid block inside this module as its child.

And the Grid.php is included which I verified by printing out something in the overriden _prepareColumns method.

What am I missing here ?

These are the contents of the Grid.php file

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

Thanks

PS. I have tried flushing the cache but no luck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2024-11-07 11:13:32

根据记忆,我认为 _prepareColumns()_prepareCollection() 之前调用,因此如果集合中存在错误,即使您已确认列,网格也不会渲染方法。

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

From memory I think _prepareColumns() is called before _prepareCollection() so if there is an error in the collection the grid won't get rendered even though you have confirmed the columns method.

Part of parent::_prepareCollection() tries to estimate the number of pages from the collection's getSize() and getSelectCountSql() methods, I often forget to check those are producing sane results which trips me up. Make sure all logging is turned on and put the following in your .htaccess file:

php_flag display_errors on
SetEnv MAGE_IS_DEVELOPER_MODE true

Try seeing what query is being generated with these commands:

Mage::log((string)$collection->getSelect());
Mage::log((string)$collection->getSelectCountSql());
弱骨蛰伏 2024-11-07 11:13:32

看来您已正确设置网格块。但是,您仍然需要将网格加载到布局中并渲染它。这可以在 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 最初比控制器中的编程实例化更难理解,但是从长远来看,它会导致更少的代码膨胀。

Looks like you have the grid blocks set up correctly. However, you still need to load the grid into the layout and render it. This can either be done in the adminhtml layout xml or in the controller.

In your /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>

In your controller:

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

Please note that you have to modify the above to your particular implementation. I think the layout xml is harder to comprehend initially than the programmatic instantiation in the controller, however, in the long run, it leads to less code bloat.

冷心人i 2024-11-07 11:13:32

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

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

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

Just had a quick view and the only thing I can see in your code is:

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

//Try to use it like this:
protected function _prepareCollection() {       
        $collection = Mage::getModel('brands/brands')->getCollection();
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文