How to add multi-select field in magento2 ui form
-
11-12-2020 - |
题
I have created a custom module in magento2. And created ui component form to save title and sort_order value, now I want to add brands as multi-select field in my module's form.
How to add a multi-select field for brands attribute options?
解决方案
Add new field in UI form
<field name="group">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">{Vendor}\{Module}\Model\Model\Source\Myvalues</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" translate="true" xsi:type="string">Your Label</item>
<item name="formElement" xsi:type="string">multiselect</item>
</item>
</argument>
</field>
Now create Myvalues.php
to provide multiselect options at
app/code/{Vendor}/{Module}/Model/Source/Myvalues.php
<?php
namespace Vendor\Module\Model\Source\Myvalues;
class Myvalues implements \Magento\Framework\Option\ArrayInterface
{
public function toOptionArray()
{
return [
['value' => 1, 'label' => __('Test One')],
['value' => 2, 'label' => __('Test Two')],
['value' => 3, 'label' => __('Test Three')],
];
}
}