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
        ));

12 Comments

  1. PDF Receitas says:

    You have brought up a very excellent details

  2. Luongsontv đồng hành cùng bạn trong mọi mùa giải.

  3. TN88 says:

    Nhà cái TN88 phù hợp với nhu cầu giải trí hiện đại.

  4. TN88 says:

    TN88 tập trung vào trải nghiệm người chơi thay vì hình thức.

  5. TN88 says:

    TN88 phù hợp với xu hướng game online.

  6. TN88 says:

    Nhà cái TN88 là địa chỉ giải trí online được quan tâm.

  7. FLY88 says:

    Nhà cái FLY88 phát triển hệ thống trò chơi hiện đại.

  8. FLY88 says:

    Nhà cái FLY88 tối ưu trải nghiệm người dùng.

  9. FLY88 says:

    Nhà cái FLY88 sở hữu độ tin cậy trong ngành giải trí trực tuyến.

  10. FLY88 says:

    FLY88 là nền tảng giải trí trực tuyến được đông đảo game thủ lựa chọn.

    https://www.circleme.com/t41a38ptskncci

  11. ta88 says:

    Trang chủ TA88 là một trong những nhà cái trực tuyến hàng đầu, cung cấp đa dạng trò chơi như cá cược thể thao, casino trực tuyến, slot game và bắn cá.

  12. ta88 says:

    TA88 là giải pháp giúp bạn thử vận may hoàn toàn tiện lợi.

Leave a Reply