Magento – How to add grid in magento admin form

adminformgridmagento-1.9

Need to added grid at highlighted place.

How i can add customer grid at highlighted place ?

Best Answer

We can display grid in admin form using custom field type

Magento admin form with grid

Form.php

<?php
class Namespace_ModuelName_Block_Adminhtml_Payment_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {
    protected function _prepareForm() {
        if (Mage::registry('data')) {
            $data = Mage::registry('data')->getData();
        } else {
            $data = array();
        }

        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('wallpetpayment_walletpayment', array('legend' => Mage::helper('customer')->__('Wallet Payment')));

        $fieldset->addType('customer_grid', 'Namespace_ModuelName_Block_Adminhtml_Payment_Edit_Form_Renderer_Fieldset_Customergrid');

        $fieldset->addField('user_id', 'customer_grid', array(
            'label'     => Mage::helper('customer')->__('Customer Name'),
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'user_id',
            'onclick' => "",
            'onchange' => "",
            'disabled' => false,
            'readonly' => false,
            'tabindex' => 1
        ));



        $fieldset->addField('amount', 'text', array(
            'label' => Mage::helper('customer')->__('Amount'),
            'class' => 'required-entry',
            'required' => true,
            'name' => 'amount',
        ));

        $wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig();
        $wysiwygConfig->addData(array('add_variables' => false,
            'add_widgets' => true,
            'add_images' => true,
            'directives_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive'),
            'directives_url_quoted' => preg_quote(Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive')),
            'widget_window_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index'),
            'files_browser_window_url' => Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index'),
            'files_browser_window_width' => (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width'),
            'files_browser_window_height' => (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height')
        ));

        $fieldset->addField('comment', 'editor', array(
            'name' => 'comment',
            'label' => Mage::helper('customer')->__('Remarks'),
            'title' => Mage::helper('customer')->__('Remarks'),
            'style' => 'width:800px; height:500px;',
            'config' => $wysiwygConfig,
            'required' => true,
            'wysiwyg' => true
        ));


        $form->setValues($data);

        return parent::_prepareForm();
    }
}

Now create Namespace\ModuleName\Block\Adminhtml\Payment\Edit\Form\Renderer\Fieldset\Customergrid.php

<?php
class Namespace_ModuelName_Block_Adminhtml_Payment_Edit_Form_Renderer_Fieldset_Customergrid extends Varien_Data_Form_Element_Abstract{
    protected $_element;

    public function getElementHtml()
    {
        return Mage::helper('core')->getLayout()->createBlock('blockname_node/adminhtml_payment_edit_form_renderer_fieldset_customer_grid')->toHtml();
    }
}

Then Create Namespace\ModuelName\Block\Adminhtml\Payment\Edit\Form\Renderer\Fieldset\Customer\Grid.php

<?php
class Namespace_ModuelName_Block_Adminhtml_Payment_Edit_Form_Renderer_Fieldset_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('customerGrid');
        $this->setUseAjax(true);
        $this->setDefaultSort('entity_id');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->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('user_id', array(
            'header'    => Mage::helper('customer')->__(''),
            'index'     => 'entity_id',
            'type' => 'radio',
            'html_name' => 'user_id',
            'align'     => 'center',
            'filter' => false
        ));

        $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'
        ));
        return parent::_prepareColumns();
    }

    public function getGridUrl()
    {
        return $this->getUrl('adminhtml/paymenthistory/customergrid', array('_current'=> true));
    }
}
Related Topic