【问题标题】:Magento : add custom button to an admin formMagento:将自定义按钮添加到管理表单
【发布时间】:2012-10-16 12:15:55
【问题描述】:

构建自定义 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).

我一直在寻找有关堆栈溢出的解决方案,但未能找到可以提供帮助的东西。然后我尝试在 Mage Core 中寻找类似的东西。

在管理面板中,如果我转到 [Catalog->Attributes->Manage Attributes] 并单击诸如“制造商”之类的标准属性,例如,在名为“管理标签/选项”的第二个选项卡上,我会看到以下屏幕:

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

Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options

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

我已经浏览了这个文件,但对我来说意义不大。我走错路了吗?我怎样才能实现类似的功能,即向管理表单添加字段的按钮和操作?

谢谢

【问题讨论】:

    标签: php magento


    【解决方案1】:
    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'));
    

    在块类中

    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;
      }
    }
    

    【讨论】:

      【解决方案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 函数中进行添加和隐藏

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-15
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-06
        • 1970-01-01
        相关资源
        最近更新 更多