我試圖在我的 CMS 頁面表單(我們創建 CMS 頁面的頁面)上為每個商店添加一個輸入欄位,但問題是它需要是動態的,我希望每個商店都顯示輸入而不是添加靜態領域。
類似于@Dhiren Vasoya 在這里所做的事情:https ://magecomp.com/blog/magento-2-add-new-field-in-admin-user-create-form/
但是到cms頁面表單。
提前感謝您的時間!
uj5u.com熱心網友回復:
這是解決方案!
步驟 1:首先,在您的表單檔案中添加以下給定代碼(在本例中為 app\code\Vendor\Extension\view\adminhtml\ui_component 檔案夾中的 cms_page_form.xml:
<fieldset name="dynamic_fieldset" class="Vendor\Extension\Ui\Component\Form\Fieldset">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Dynamic Fields</item>
<item name="sortOrder" xsi:type="number">1000</item>
</item>
</argument>
</fieldset>
第 2 步:在上述步驟之后,在 app\code\Vendor\Extension\Ui\Component\Form 檔案夾中創建 Fieldset.php 檔案并添加以下給定代碼:
<?php
namespace Vendor\Extension\Ui\Component\Form;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Form\FieldFactory;
class Fieldset extends \Magento\Ui\Component\Form\Fieldset
{
protected $fieldFactory;
public function __construct(
ContextInterface $context,
array $components = [],
array $data = [],
FieldFactory $fieldFactory)
{
parent::__construct($context, $components, $data);
$this->fieldFactory = $fieldFactory;
}
public function getChildComponents()
{
$options = [
0 => [
'label' => 'Option 1',
'value' => 1
],
1 => [
'label' => 'Option 2',
'value' => 2
],
2 => [
'label' => 'Option 3',
'value' => 3
],
];
$fields = [
[
'label' => __('Label'),
'value' => __('Label Value'),
'formElement' => 'input',
],
[
'label' => __('Checkbox'),
'value' => __('0'),
'formElement' => 'checkbox',
],
[
'label' => __('Dropdown'),
'options' => $options,
'formElement' => 'select',
],
];
foreach ($fields as $key => $field) {
$fieldInstance = $this->fieldFactory->create();
$name = 'custom_field_' . $key;
$fieldInstance->setData(
[
'config' => $field,
'name' => $name
]
);
$fieldInstance->prepare();
$this->addComponent($name, $fieldInstance);
}
return parent::getChildComponents();
}
}
第 3 步:最后,按照上述步驟清除快取。
抄送:https : //magecomp.com/blog/dynamically-add-fields-in-custom-admin-form-using-ui-component/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357685.html
上一篇:帶有TreeViewItem.Header的TreeViewItem上的C#WPFTreeViewItemMouseDoubleClick事件
