开发者

Magento Custom Backend module Grid sort/filter problem

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?


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:

<events_codes_edit_customergrid>

I am guessing this should say

<events_codes_customergrid>

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜