简体   繁体   English

如何在magento2管理表单中添加动态字段

[英]how to add dynamic field in magento2 admin form

I want to implement dynamic Form field in magento2 我想在magento2中实现动态Form字段

in which user can add as much as images as per requirement just like add more option , 用户可以根据需要添加尽可能多的图像,就像添加更多选项一样,

I have implemented this by adding 10 new images on edit but user is not able to add more images on the way 我通过在编辑时添加10张新图像来实现此目的,但是用户无法在此途中添加更多图像

after form i have written this 表格写完后

$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();

let me know if this can be done more Dynamic just like on click a button Add Image new field is added in form 让我知道这是否可以做得更动态就像单击按钮一样在表单中添加“添加图像”新字段

you have to create a js function to generate the files that you need, i think you can use this function to add an script block to the form: 您必须创建一个js函数来生成所需的文件,我认为您可以使用此函数向表单添加脚本块:

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

and you can create a script to add dynamic fields via javascript 您可以创建脚本以通过javascript添加动态字段

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

I hope this solution hepl you, i don't know if is the correct way, but this works 我希望这个解决方案能帮助您,我不知道这是否是正确的方法,但是这种方法有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM