Magento自定义后端模块网格排序/过滤问题
我正在开发一个自定义管理模块,在该模块中我根据自定义属性显示客户列表,网格加载正常,但每当我尝试对网格进行排序/过滤时,我都会遇到问题。
这是我收到的错误:
Fatal error: Call to a member function toHtml() on a non-object in <b>/***/***/public_html/***/app/code/local/BelVG/Events/controllers/CodesController.php</b> on line <b>28</b>
这是导致 CodesController 文件中出现错误的代码:
public function customerGridAction() {
$this->loadLayout();
$this->getResponse()->setBody($this->getLayout()->getBlock('events.codes.edit.customer')->toHtml());
}
XML 布局文件:
<events_codes_edit>
<reference name="content">
<block type="events/codes_edit" name="events.codes.edit" template="events/codes/edit.phtml">
<block type="events/codes_edit_customer" name="events.codes.edit.customer" as="customer"/>
</block>
</reference>
</events_codes_edit>
<events_codes_edit_customergrid>
<remove name="root"/>
<block type="events/codes_edit_customer" name="events.codes.edit.customer" as="events.codes.edit.customer"/>
</events_codes_edit_customergrid></code>
网格的类文件:
class BelVG_Events_Block_Codes_Edit_Customer extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct() {
parent::__construct();
$this->setId('events_codes_edit_product');
$this->setUseAjax(true);
$this->setDefaultSort('entity_id');
$this->setDefaultDir('asc');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$current_code = Mage::registry('current_code');
$code = $current_code->getCode();
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('i_code')
->addAttributeToSelect('gender')
->addFieldToFilter('i_code', $code)
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number'
));
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode'
));
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'index' => 'billing_country_id'
));
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region'
));
$this->addColumn('gender', array(
'header' => Mage::helper('customer')->__('Gender'),
'align' => 'center',
'index' => 'gender'
));
return parent::_prepareColumns();
}
public function getGridUrl() {
return $this->getUrl('*/*/customergrid', array('_current'=> true));
}
}
首先调用此网格在另一个块(即 Edit.php)内,从模板文件 edit.phtml 调用它
Edit.php 块类:
class BelVG_Events_Block_Codes_Edit extends Mage_Core_Block_Template {
public function getGridHtml() {
return $this->getChild('customer')->toHtml();
}
}
在 edit.phtml 内调用客户网格的代码 :
<div id="" class="fieldset">
<div class="hor-scroll">
<?php echo $this->getGridHtml() ?>
</div>
</div>
我不知道为什么会发生这种情况,我已经仔细检查了控制器中的块名称和布局文件,它们似乎是匹配的,我什至尝试使用 createBlock() 而不是 getBlock() 并直接指向块文件,但仍然显示完全相同的错误。
有人能指出我正确的方向吗?
I am working on a custom admin module where I show a list of customers based on a custom attribute, the grid loads fine but then I am having problems whenever I try to sort/filter the grid.
This is the error I am getting :
Fatal error: Call to a member function toHtml() on a non-object in <b>/***/***/public_html/***/app/code/local/BelVG/Events/controllers/CodesController.php</b> on line <b>28</b>
This is the code that causes the error in the CodesController file :
public function customerGridAction() {
$this->loadLayout();
$this->getResponse()->setBody($this->getLayout()->getBlock('events.codes.edit.customer')->toHtml());
}
XML layout file:
<events_codes_edit>
<reference name="content">
<block type="events/codes_edit" name="events.codes.edit" template="events/codes/edit.phtml">
<block type="events/codes_edit_customer" name="events.codes.edit.customer" as="customer"/>
</block>
</reference>
</events_codes_edit>
<events_codes_edit_customergrid>
<remove name="root"/>
<block type="events/codes_edit_customer" name="events.codes.edit.customer" as="events.codes.edit.customer"/>
</events_codes_edit_customergrid></code>
Class file for the Grid:
class BelVG_Events_Block_Codes_Edit_Customer extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct() {
parent::__construct();
$this->setId('events_codes_edit_product');
$this->setUseAjax(true);
$this->setDefaultSort('entity_id');
$this->setDefaultDir('asc');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$current_code = Mage::registry('current_code');
$code = $current_code->getCode();
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('i_code')
->addAttributeToSelect('gender')
->addFieldToFilter('i_code', $code)
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number'
));
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode'
));
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'index' => 'billing_country_id'
));
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region'
));
$this->addColumn('gender', array(
'header' => Mage::helper('customer')->__('Gender'),
'align' => 'center',
'index' => 'gender'
));
return parent::_prepareColumns();
}
public function getGridUrl() {
return $this->getUrl('*/*/customergrid', array('_current'=> true));
}
}
This grid is first called inside another block, which is Edit.php, and it's called from a template file edit.phtml
Edit.php block class:
class BelVG_Events_Block_Codes_Edit extends Mage_Core_Block_Template {
public function getGridHtml() {
return $this->getChild('customer')->toHtml();
}
}
Code for calling the customer grid inside edit.phtml:
<div id="" class="fieldset">
<div class="hor-scroll">
<?php echo $this->getGridHtml() ?>
</div>
</div>
I have no idea why this is happening, I have double checked the block name in the controller, and the layout files and they seem to be matching, I even tried to use createBlock() instead of getBlock() and pointed directly to the block file, but still it showed the exact same error.
Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你的模块/控制器/操作路径是什么样的,但我想说你在 xml 中犯了一些错误:
我猜这应该说
但是无论如何。如果您想获取某个特定块的 html,则这并不是必需的。我建议做的是,使用布局类创建块然后显示它。
这通常适用于所有网格块,因为它们在类中定义了 Collection 实例。
所以尝试使用
I am not sure what your module/controller/action path looks like, but I would say that you have made something wrong in your xml at:
I am guessing this should say
But anyhow. this is not really required if you want to get the html for one particular block. What I recommend doing is, use the layout class to create the block and then show it.
This usually works for all grid blocks, as they have the Collection instance defined inside the class.
so try using