2

I have to develop the functionality like Add Custom Model Grid Into Another Custom Model Edit Tab. I Have two table with grid generated. I successfully add the Tab In my custom grid.

But Bit confuse How to assign that another grid into this grid. Also I have to put the Ajax on this tab.

If anybody is Done Before this then please guide me.

Help will be Appropriated.

  • It should work the same as described here magento.stackexchange.com/a/13309/146. Just replace product with your other entity. – Marius Jan 31 '14 at 13:47
  • @Marius Let me check.. – Keyul Shah Jan 31 '14 at 13:48
  • I don't want to create the different table in that and just display grid and provide filtering with ajax. your previous code we create the two different table and make grid serialization. – Keyul Shah Jan 31 '14 at 13:52
  • You mean your backend should contains model edit form with 2 grids? Structure will be tabs[tab1[model_edit], tab2[grid_1],tab3[grid_2]]. Do I uderstand correctly? – mageUz Jan 31 '14 at 14:34
  • not edit the grid but one tab contain editable grid data. Second tab contain only just listing of the different model grid with filtration. when you save the data just save only first tab second tab just only for listing – Keyul Shah Jan 31 '14 at 19:04
1

Following I give some example which I have done before. Mine contains one edit form and one ajax grid, but with this logic, you can add any number of tabs and grids in this form. File structuire:

enter image description here

1./app/code/local/Ssd/Experts/Block/Adminhtml/Experts.php
/**
 * Experts management Grid container, row click opents expert edit page
 */
class Ssd_Experts_Block_Adminhtml_Experts extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function _construct()
    {
        $this->_controller = 'adminhtml_experts';
        $this->_blockGroup = 'experts';
        $this->_headerText = Mage::helper('experts')->__('Manage experts');
        $this->_addButtonLabel = Mage::helper('brands')->__('Add Expert');
        parent::_construct();
    }
}

2./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Grid.php

class Ssd_Experts_Block_Adminhtml_Experts_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('expertsGrid');
        $this->setDefaultSort('name');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('experts/experts')->getCollection()->addCategoryData();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {

        $this->addColumn('expert_name', array(
                                           'header' => Mage::helper('experts')->__('Name'),
                                           'align' => 'left',
                                           'index' => 'expert_name',
                                           'width' => '250',
                                      ));
        /**
         * another columns here
         */

        return parent::_prepareColumns();
    }
}

3./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit.php

/**
 * Expert edit form container
 */
class Ssd_Experts_Block_Adminhtml_Experts_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        parent::__construct();
        $this->_objectId = 'id';
        $this->_blockGroup = "experts";
        $this->_controller = "adminhtml_experts";

        $this->updateButton('delete', 'label', Mage::helper('experts')->__('Delete Expert'));
        $this->updateButton('save', 'label', Mage::helper('experts')->__('Save Expert'));

    }
}

4./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tabs.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('experts_tab');
        $this->setDestElementId('edit_form');
    }

    protected function _beforeToHtml()
    {
        //edit form tab
        $this->addTab('expert_form_edit', array(
            'label' => Mage::helper('brands')->__('Edit Expert'),
            'title' => Mage::helper('brands')->__('Edit Expert'),
            'content' => $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_form')->toHtml(),
            'active' => true
        ));

        if (Mage::registry('experts_data') && Mage::registry('experts_data')->getId()) {
            //grid tab
            $this->addTab('expert_advices', array(
                'label' => Mage::helper('experts')->__('Expert Advices'),
                'title' => Mage::helper('experts')->__('Expert Advices'),
                'content' => $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_advices_grid')->toHtml(),
            ));
        }

        //also you can add another grid tab
        /*$this->addTab('expert_advices', array(
            'label' => Mage::helper('experts')->__('Another Grid'),
            'title' => Mage::helper('experts')->__('Another Grid'),
            'content' => $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_another_grid')->toHtml(),
        ));*/

        return parent::_beforeToHtml();
    }
}

5./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Form.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data',
        ));
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

6./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tab/Form.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{

    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();

        $fieldset = $form->addFieldset('personal_information', array(
            'legend' => Mage::helper('experts')->__('Personal Information'),
            'class' => 'fieldset-wide',
        ));

        $fieldset->addField('expert_name', 'text', array(
            'label' => Mage::helper('experts')->__('Expert Name'),
            'required' => true,
            'name' => 'expert_name',
        ));
        /**
         * another elements here
         * this is typical edit form
         */
        return parent::_prepareForm();
    }
}

7./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tab/Advices/Grid.php
/**
 * this is typical grid, but it also impelents tab interface
 */
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tab_Advices_Grid extends Mage_Adminhtml_Block_Widget_Grid
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
    public function _construct()
    {
        parent::_construct();
        $this->setId('expert_advice_grid');
        $this->setDefaultSort('page_title');
        $this->setDefaultDir('ASC');
        //enable ajax grid
        $this->setUseAjax(true);
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('experts/advices')
            ->getCollection()
            ->addExpertData()
            ->addFieldToFilter('main_table.expert_id', Mage::app()->getRequest()->getParam('id'));
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {

        $this->addColumn('advice_title', array(
            'header' => Mage::helper('experts')->__('Title'),
            'align' => 'left',
            'index' => 'advice_title',
        ));

        /**
         * add another columns here
         */
        return parent::_prepareColumns();
    }

    //this method is reuired if you want ajax grid
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current' => true));
    }


    public function canShowTab()
    {
        return true;
    }

    public function isHidden()
    {
        return false;
    }

    public function getTabLabel()
    {
        return $this->__('Settings');
    }

    public function getTabTitle()
    {
        return $this->__('Expert Advices');
    }

}

8./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tab/Another/Grid.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tab_Another_Grid extends Mage_Adminhtml_Block_Widget_Grid
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
    public function _construct()
    {
        parent::_construct();
        $this->setId('expert_another_grid');
        //enable ajax grid
        $this->setUseAjax(true);
    }

    protected function _prepareCollection()
    {
        /**
         * prepare collection what you want
         */
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        /**
         * add another columns here
         */
        return parent::_prepareColumns();
    }

    //this method is reuired if you want ajax grid
    public function getGridUrl()
    {
        return $this->getUrl('*/*/anothergrid', array('_current' => true));
    }

    /**
     * implement tab interface methods
     */
}

9./app/code/local/Ssd/Experts/controllers/Adminhtml/ExpertsController.php

class Ssd_Experts_Adminhtml_ExpertsController extends Mage_Adminhtml_Controller_Action
{
    //this is ajax grid request action
    public function gridAction()
    {
        echo $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_advices_grid')->toHtml();
    }

    public function editAction()
    {
        $expertModel = Mage::getModel('experts/experts')->load($this->getRequest()->getParam('id'));
        if ($expertModel->getId()) {
            Mage::register('experts_data', $expertModel);
            $this->loadLayout();
            //attention here, what blocks are creating, left is tab, content is edit container
            $this->_addLeft($this->getLayout()->createBlock('experts/adminhtml_experts_edit_tabs'));
            $this->_addContent($this->getLayout()->createBlock('experts/adminhtml_experts_edit'));
            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('experts')->__('Expert does not exist'));
            $this->_redirect('*/*/');
        }
    }

    /**
     * another actions here: index, delete, mass....
     */

}

I hope you can understand file structure using class names. some codes are cutted out. My experts edit form contains edit form container, tabs, end each tab content is can be form controls, grid or any content what you want. Additional comments are in the code. Hope this example helps you.

  • I will try this and let you know. – Keyul Shah Feb 3 '14 at 4:36
  • Yes I try but I got solution with in just add 6 line of code. only – Keyul Shah Feb 3 '14 at 5:28
  • @keyul Shah, Will you please provide me that 6 line code, its very urgent for me – amit_game Jun 11 '15 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.