Display grid in admin form using custom field type
Step 1. Create new field type. Add the file app/local/code/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit/Form/Renderer/Fieldset/Productgrid.php
<?php class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form_Renderer_Fieldset_Productgrid extends Varien_Data_Form_Element_Abstract { protected $_element; public function getElementHtml() { return Mage::helper('core')->getLayout()->createBlock('modulename/adminhtml_modulename_edit_form_renderer_fieldset_product_grid')->toHtml(); } } |
Step 2. Add Grid to app/local/code/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit/Form/Renderer/Fieldset/Product/Grid.php
<?php class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form_Renderer_Fieldset_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId('productsId'); $this->setDefaultSort('id'); $this->setUseAjax(true); } protected function _prepareCollection() { $collection = Mage::getModel('catalog/product')->getCollection() ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED) $this->setCollection($collection); return parent::_prepareCollection(); } protected function _prepareColumns() { $this->addColumn('grid_id', array( 'header' => Mage::helper('modulename')->__(''), 'index' => 'entity_id', 'type' => 'radio', 'html_name' => 'grid_id', 'align' => 'center', 'filter' => false, )); $this->addColumn('id', array( 'header' => Mage::helper('modulename')->__('ID'), 'sortable' => true, 'width' => '60px', 'index' => 'entity_id' )); $this->addColumn('product_name', array( 'header' => Mage::helper('modulename')->__('Name'), 'index' => 'name' )); $this->addColumn('sku', array( 'header' => Mage::helper('modulename')->__('SKU'), 'width' => '120px', 'index' => 'sku' )); return parent::_prepareColumns(); } public function getGridUrl() { return $this->getUrl('*/*/product', array('_current' => true)); } } |
Step 3. Add custom field to your Form in the file app/local/code/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit/Form.php
<?php class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { ... $fieldsetData->addType('product_grid', 'Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form_Renderer_Fieldset_Productgrid'); $fieldsetData->addField('product_id_grid', 'product_grid', array( 'name' => 'product_id_grid', 'label' => Mage::helper('modulename')->__('Product'), 'class' => 'required-entry', 'required' => true, 'onclick' => "", 'onchange' => "", 'disabled' => false, 'readonly' => false, 'tabindex' => 1 )); |