【问题标题】:Magento Custom Backend module Grid sort/filter problemMagento 自定义后端模块网格排序/过滤问题
【发布时间】:2010-11-13 00:05:53
【问题描述】:

我正在开发一个自定义管理模块,在该模块中我根据自定义属性显示客户列表,网格加载正常,但是每当我尝试对网格进行排序/过滤时都会遇到问题。

这是我得到的错误:

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() 并直接指向到块文件,但它仍然显示完全相同的错误。

谁能指出我正确的方向?

【问题讨论】:

  • 也许你可以 var_dump() 或检查 getLayout() 的返回值,看看里面有哪些块可用。
  • @Amr@butterbrot : 你有任何教程链接可以在模块页面中制作网格视图吗?请分享

标签: php zend-framework magento


【解决方案1】:

我不确定您的模块/控制器/操作路径是什么样的,但我会说您在 xml 中出错了:

<events_codes_edit_customergrid>

我猜这应该说

<events_codes_customergrid>

但无论如何。如果您想获取一个特定块的 html,这并不是真正需要的。我建议做的是,使用布局类创建块然后显示它。

这通常适用于所有网格块,因为它们在类中定义了 Collection 实例。

所以尝试使用

$this->getResponse()->setBody($this->getLayout()->createBlock('events/codes_edit_customer')->toHtml());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2015-09-24
    • 1970-01-01
    相关资源
    最近更新 更多