Magento:向管理表单添加自定义按钮

时间:2012-10-16 12:15:55

标签: php magento

构建自定义adminhtml模块。我用一个简单的表格。它看起来像这样:

<?php

class Namespace_Modulename_Block_Adminhtml_Modulename_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('modulename_form',array('legend'=>Mage::helper('modulename')->__('Module Data')));


    $fieldset->addField('test', 'text', array(
            'label'     => Mage::helper('modulename')->__('Test'),
            'name'      => 'test',
    ));

    // I want to add a custom button here. Say an action called "Add Option".
    //Clicking this action adds input boxes or dropdowns to the form that are to
    //to be included in the post when submitting the form (obviously).

我一直在寻找Stack溢出的解决方案,但却找不到可能有用的东西。然后我尝试在Mage Core中搜索类似的东西。

在管理面板中,如果我转到[目录 - &gt;属性 - >管理属性]并点击标准属性,例如“制造商”,在第二个标签上,名为“管理标签/选项”我请参见以下屏幕:

add options

有一个按钮和操作,允许我向表单添加选项(以文本框的形式)。将此识别为我想要复制的东西,我进入核心试图弄清楚要做什么。如果我打开以下文件( Magento Enteprise 12.0.2 ):

Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options

我看到它是空的,但扩展了Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract

我已经浏览了这个文件,但对我来说没什么用。我走错了路吗?如何实现类似的功能,按钮和操作,将字段添加到管理表单?

感谢

2 个答案:

答案 0 :(得分:7)

  1. 您可以为表单设置自定义模板,并通过html添加任何标签(如按钮)。
  2. 您可以为字段添加渲染器
  3. $customField = $fieldset->addField('test', 'text', array(
                'label'     => Mage::helper('modulename')->__('Test'),
                'name'      => 'test',
        ));
    
    $customField->setRenderer($this->getLayout()->createBlock('yuormodule/adminhtml_yourform_edit_renderer_button'));
    
    块类中的

    class Yournamespace_Yourmodule_Block_Adminhtml_Yourform_Edit_Renderer_Button extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface {
      public function render(Varien_Data_Form_Element_Abstract $element) {
        //You can write html for your button here
        $html = '<button></button>';
       return $html;
      }
    }
    

答案 1 :(得分:2)

1-首先,你必须为你的网格创建一个容器

public function __construct() {        
    parent::__construct();
    $this->setTemplate('markavip/dataflow/edit/form/mapping.phtml');
}

2-在同一个容器中,你将添加你的按钮,如

public function _prepareLayout() {
    $this->setChild('add_button', $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setData(array(
                        'label' => Mage::helper('markavip_dataflow')->__('Add Option'),
                        'class' => 'add',
                        'id' => 'add_new_option_button'
    )));
    $this->setChild('delete_button', $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setData(array(
                        'label' => Mage::helper('markavip_dataflow')->__('Delete'),
                        'class' => 'delete delete-option'
    )));
    return parent::_prepareLayout();
}

public function getAddNewButtonHtml() {
    return $this->getChildHtml('add_button');
}

public function getDeleteButtonHtml() {
    return $this->getChildHtml('delete_button');
}

3-并在PHTML中添加如下按钮:

 <?php echo $this->getAddNewButtonHtml() ?>

在此之后,您将使用JS函数进行添加和隐藏