在这一章节里,我们将学习如何将 Grid 表正确的添加至后台表单选项卡所对应的 content 区块里
这一章节只针对于对 Magento 有深刻了解的开发者,并已成功在后台选项卡所对应的 Content
区块里添加过 Grid 表,及有能力解决所碰到的问题
为了更好的讲解,我们来拿一个客户的需求当作实例,需求方案如下,建立一个实体叫’Customer Manager’,
随之分配一批客户给这个’Customer Manager’来管理,在后台页面,
我们需要能够从整个客户表里选择客户,然后保存或者关系到某一个指定的
Customer Manager。所以在’Customer Manager’编辑页面中要显示出一个完整
的客户 Grid 表, 这个表里会有 checkbox 列可以多选客户,
这个类似于在后台编辑商品页面里的 Related Products 和 Cross-sell Products
默认或者简单的操作方法是: 在 Tabs.php 文件里创建一个后台视窗,这里有个 addTab() 方法来定义我们想要添加的视窗路径(指: block 文件路径),代码如下:
$this->addTab('form_section', array(
'label' => Mage::helper('employee')->__('Sample Grid'),
'title' => Mage::helper('employee')->__('Sample Grid'),
'content' => $this->getLayout()
->createBlock('employee/adminhtml_fav_edit_tab_grid')
->toHtml(),
));
这样一个 Grid 表会显示在新增选项卡(Tab)所对应的 Content 区块里, 但是在运行的时候会有许多问题产生。比如在搜索或分页的时候,和添加/删除条目有关操作的时候.
正确的操作方法
在 Tabs.php 中的 addTab() 方法应该如下:
$this->addTab('form_section5', array(
'label' => Mage::helper('employee')->__('Customers'),
'title' => Mage::helper('employee')->__('Customers'),
'url' => $this->getUrl('*/*/customer', array('_current' => true)),
'class' => 'ajax',
));
这样做的话,当你点击 Customers Tab, 它就会以 ajax 方式调用 url 加载,然后显示出 Grid 表
在我们的 controller 文件中, 添加 custoemrAction() 方法,代码如下:
public function customerAction()
{
$this->loadLayout();
$this->getLayout()->getBlock('customer.grid')
->setCustomers(
$this->getRequest()->getPost('customers', null)
);
$this->renderLayout();
}
这个操作十分简单, 只是简单的加载和输出 Layout, 所以还有段重要的代码应该写在该模块的 XML 文件里(在 Layout 文件夹下), 如下:
http://www.sunzhenghua.com/magento-admin-module-development-part6-internal-grid-forms-advanced