I have created a Ui form in magento 2. How do i kept status as dropdown list in Ui form ? It should look in below format. Status-1)Enabled 2)Disabled
2 Answers
Try this code, it will show toggle for yes/no values
<field name="is_active" sortOrder="10" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">homeslider</item>
<item name="default" xsi:type="number">1</item>
</item>
</argument>
<settings>
<dataType>boolean</dataType>
<label translate="true">Status</label>
<dataScope>status</dataScope>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="number">0</map>
<map name="true" xsi:type="number">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
change field name as per your form
Update: For dropdown use below code
<field name="status" formElement="select">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">homeslider</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">Status</label>
<dataScope>status</dataScope>
</settings>
<formElements>
<select>
<settings>
<options class="Magento\Config\Model\Config\Source\Yesno"/>
</settings>
</select>
</formElements>
</field>
Update: to change value from Yes/No to Enable/Disable Change
<options class="Magento\Config\Model\Config\Source\Yesno"/>
To
<options class="Magento\Config\Model\Config\Source\Enabledisable"/>
-
Yes,It can work . But i need it as dropdown list which consists two options , enabled and disabled– JaisaCommented Nov 16, 2017 at 11:32
-
You mistakenly understand. I need dropdown list not like the above one. Both of your codes shows same output what i get before– JaisaCommented Nov 16, 2017 at 11:43
-
-
-
Go for this option
<field name="active">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">30</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">checkbox</item>
<item name="prefer" xsi:type="string">toggle</item>
<item name="label" xsi:type="string" translate="true">Active</item>
<item name="dataScope" xsi:type="string">active</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="source" xsi:type="string">general</item>
<item name="valueMap" xsi:type="array">
<item name="true" xsi:type="string">1</item>
<item name="false" xsi:type="string">0</item>
</item>
<!--<item name="validation" xsi:type="array">-->
<!--<item name="required-entry" xsi:type="boolean">false</item>-->
<!--</item>-->
<item name="default" xsi:type="string">1</item>
</item>
</argument>
</field>