【问题标题】:how to add dynamic field in magento2 admin form如何在 magento2 管理表单中添加动态字段
【发布时间】:2016-12-18 14:04:31
【问题描述】:

我想在magento2中实现动态表单字段

用户可以根据需要添加尽可能多的图像,就像添加更多选项一样,

我通过在编辑时添加 10 个新图像来实现这一点,但用户无法在途中添加更多图像

在表格之后我写了这个

$newfieldset = $form->addFieldset('new_fieldset', ['legend' => __('New Images #')]);
    foreach (range(0, 10) as $newid) {      
    $newfieldset->addField(
        "newimages_$newid",
        'image',
        ['name' => 'galleryimages[]', 'label' => __('New Image #'.$newid), 'title' => __('New Image #'.$newid), 'required' => false ,  'disabled' => $isElementDisabled]
    );
}
$this->setForm($form);
return parent::_prepareForm();

让我知道这是否可以像点击按钮一样动态地完成添加图片新字段添加到表单中

【问题讨论】:

    标签: javascript admin magento2


    【解决方案1】:

    你必须创建一个 js 函数来生成你需要的文件,我认为你可以使用这个函数在表单中添加一个脚本块:

    $this->setChild('form_after',$this->getLayout()->createBlock('Magento\Framework\View\Element\Template')->setTemplate('Vendor_Company::folder/js.phtml'));
    

    您可以创建一个脚本来通过 javascript 添加动态字段

    <script>
        require([
        "jquery",
        "mage/adminhtml/form", // in case you need some of the magento functions
        'domReady!',
    ], function ($) {
         //YOUR CODE HERE ...
    });
    

    我希望这个解决方案能帮到你,我不知道是否是正确的方法,但这有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-03
      • 2014-08-05
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多