网格不会出现在 Magento 的自定义管理模块中
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据记忆,我认为
_prepareColumns()
在_prepareCollection()
之前调用,因此如果集合中存在错误,即使您已确认列,网格也不会渲染方法。parent::_prepareCollection()
的一部分尝试从集合的getSize()
和getSelectCountSql()
方法估计页数,我经常这样做忘记检查那些是否产生了合理的结果,这让我很困惑。确保所有日志记录均已打开,并将以下内容放入.htaccess
文件中:尝试查看使用这些命令生成的查询:
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'sgetSize()
andgetSelectCountSql()
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:Try seeing what query is being generated with these commands:
看来您已正确设置网格块。但是,您仍然需要将网格加载到布局中并渲染它。这可以在 adminhtml 布局 xml 或控制器中完成。
在您的 /app/design/adminhtml/../layout/brands.xml 中:
在您的控制器中:
请注意,您必须将上述内容修改为您的特定实现。我认为布局 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:
In your controller:
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.
刚刚快速浏览了一下,我在您的代码中唯一能看到的是:
Just had a quick view and the only thing I can see in your code is: