1

我想编写一个带有自定义实体的模块。在后端,它应该看起来像产品的后端(左侧的选项卡,右侧的表单)。

我尝试了许多变体,并从核心检查/复制了许多东西以理解它……好吧,我没有。

知道任何人的教程或实现这一点的必要关键点吗?

非常感谢

编辑:好吧,创建自己的实体不是问题,这是众所周知的。我需要帮助来创建后端,以便在编辑产品时结果看起来像选项卡式表单

4

1 回答 1

9

要在 admin 中添加多个选项卡,请首先通过mpaepper 提供的http://codemagento.com/2011/02/grids-and-forms-in-the-admin-panel/ 。

之后创建下面的类

Super_Awesome_Block_Adminhtml_Example_Edit_Tabs
Super_Awesome_Block_Adminhtml_Example_Edit_Tabs_Form
Super_Awesome_Block_Adminhtml_Example_Edit_Tabs_SecondTab

并修改 Super_Awesome_Block_Adminhtml_Example_Edit_Form

class Super_Awesome_Block_Adminhtml_Example_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();
    }
}

添加以下代码

class Super_Awesome_Block_Adminhtml_Example_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs {

    public function __construct() {
        parent::__construct();
        $this->setId('awesome_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('awesome')->__('Your Title Here'));
    }

    protected function _beforeToHtml() {
        $this->addTab('form_section', array(
            'label' => Mage::helper('awesome')->__('Details'),
            'title' => Mage::helper('awesome')->__('Details'),
            'content' => $this->getLayout()->createBlock('awesome/adminhtml_awesome_edit_tab_form')->toHtml(),
        ));


        $this->addTab('secondtab_section', array(
            'label'     => Mage::helper('awesome')->__('SecondTab'),
            'title' => Mage::helper('awesome')->__('SecondTab'),
            'content'   => $this->getLayout()->createBlock('awesome/adminhtml_awesome_edit_tab_secondtab')->toHtml(),
        ));

        return parent::_beforeToHtml();
    }

}

...

class Super_Awesome_Block_Adminhtml_Example_Edit_Tabs_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
      $form = new Varien_Data_Form();
      $this->setForm($form);
      $fieldset = $form->addFieldset('awesome_form', array('legend'=>Mage::helper('awesome')->__('Header text here')));

      $fieldset = $form->addFieldset('example_form', array(
             'legend' =>Mage::helper('awesome')->__('Example Information')
        ));

        $fieldset->addField('name', 'text', array(
             'label'     => Mage::helper('awesome')->__('Name'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'name',
             'note'     => Mage::helper('awesome')->__('The name of the example.'),
        ));

        $fieldset->addField('description', 'text', array(
             'label'     => Mage::helper('awesome')->__('Description'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'description',
        ));

        $fieldset->addField('other', 'text', array(
             'label'     => Mage::helper('awesome')->__('Other'),
             'class'     => 'required-entry',
             'required'  => true,
             'name'      => 'other',
        ));

        if (Mage::getSingleton('adminhtml/session')->getExampleData())
        {
            $data = Mage::getSingleton('adminhtml/session')->getExamplelData();
            Mage::getSingleton('adminhtml/session')->getExampleData(null);
        }
        elseif (Mage::registry('example_data'))
        {
            $data = Mage::registry('example_data')->getData();
        }
        else
        {
            $data = array();
        }

      return parent::_prepareForm();
  }
}

……

class Super_Awesome_Block_Adminhtml_Example_Edit_Tabs_SecondTab extends Mage_Adminhtml_Block_Widget_Grid {

    public function __construct() {
        parent::__construct();
        $this->setId('awesomeGrid');
        $this->setDefaultSort('awesome_secondtab_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setFilterVisibility(false);
        $this->setPagerVisibility(false);
    }

    protected function _prepareCollection() {
        $id     = $this->getRequest()->getParam('id');
        $collection = Mage::getModel('awesome/secondtab')->getCollection()->addFilter('awesome_id', $id);
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns() {


        $this->addColumn('created_time', array(
            'header' => Mage::helper('awesome')->__('Date'),
            'index' => 'created_time',
            'type' => 'datetime',
            'align' => 'left',
            'sortable' => false,
        ));

        $this->addColumn('type', array(
            'header' => Mage::helper('awesome')->__('Type'),
            'align' => 'left',
            'index' => 'type',
            'sortable' => false,
        ));

        $this->addColumn('amount', array(
            'header' => Mage::helper('awesome')->__('Amount'),
            'align' => 'left',
            'index' => 'amount',
            'type'  => 'currency',
            'currency' => 'amount',
            'sortable' => false,
        ));

        $this->addColumn('balance', array(
            'header' => Mage::helper('awesome')->__('Balance'),
            'align' => 'left',
            'index' => 'balance',
            'type'  => 'currency',
            'currency' => 'balance',
            'sortable' => false,
        ));

        $this->addColumn('order_number', array(
            'header' => Mage::helper('awesome')->__('Order Number'),
            'align' => 'left',
            'index' => 'order_number',
            'sortable' => false,
        ));

        return parent::_prepareColumns();
    }

}
于 2013-01-04T05:38:13.827 回答