magento tabbed后端,如自定义实体的目录/产品

时间:2013-01-03 18:50:46

标签: magento magento-1.7 adminhtml

我想用自定义实体编写一个模块。在后端,它看起来像产品的后端(左侧的标签,右侧的表格)。

我尝试了很多变种,并从核心检查/复制了许多东西以便理解它......我不这样做。

知道任何人有教程或必要的关键点来实现这个目标吗?

非常感谢

编辑:嗯,创建自己的实体不是问题,这是众所周知的。 我需要帮助来创建后端,以便在编辑产品时结果看起来像标签形式

1 个答案:

答案 0 :(得分:9)

要在管理员中添加多个标签,请先浏览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();
    }

}