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

act*_*Dev 1 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).
Run Code Online (Sandbox Code Playgroud)

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

在管理面板中,如果我转到[目录 - >属性 - >管理属性]并单击标准属性,例如"制造商",在第二个选项卡上,名为"管理标签/选项",我会看到以下屏幕:

添加选项

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

Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options

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

我已经浏览了这个文件,但对我来说没什么用.我走错了路吗?我怎样才能实现类似的东西,一个按钮和动作,为管理表单添加字段?

谢谢

小智 7

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

$customField->setRenderer($this->getLayout()->createBlock('yuormodule/adminhtml_yourform_edit_renderer_button'));
Run Code Online (Sandbox Code Playgroud)

在Block Class中

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;
  }
}
Run Code Online (Sandbox Code Playgroud)