cancel
Showing results for 
Search instead for 
Did you mean: 

Ui Component - display sub options when parent option is selected

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Ui Component - display sub options when parent option is selected

I would like to display sub options when a parent option was selected.

Example:

 

Dropdown 1:

Display  all attributes available as options

 

When selected "manufacturer"

 

Dropdown 2:

Display options under manufacturer, such as "Toyota", "Ford", "Isuzu"

 

Would you give me suggestions? Should we use custom js code?

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Ui Component - display sub options when parent option is selected

Just realised that when the field is not visible it doesn't define the field.

 

View solution in original post

6 REPLIES 6

Re: Ui Component - display sub options when parent option is selected

 

 

<field name="field1">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Namespace\ModuleName\Model\Config\Source\Options</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Parent Option</item>
            <item name="component" xsi:type="string">Namespace_ModuleName/js/form/element/options</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="dataType" xsi:type="string">number</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="source" xsi:type="string">item</item>
            <item name="dataScope" xsi:type="string">field1</item>
            <item name="sortOrder" xsi:type="number">210</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
        </item>
    </argument>
</field>

<field name="field2Depend1">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Field 1</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">item</item>
            <item name="sortOrder" xsi:type="number">220</item>
            <item name="breakLine" xsi:type="boolean">true</item>
            <item name="visibleValue" xsi:type="string">2</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</field>
<field name="field3Depend1">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Field 2</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">item</item>
            <item name="sortOrder" xsi:type="number">230</item>
            <item name="breakLine" xsi:type="boolean">true</item>
            <item name="visibleValue" xsi:type="string">0</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</field>
  • The child elements visibility is set by default as false;
  • The visibleValue - is field1 value when element should be visible;

Namespace\ModuleName\Model\Config\Source\Options

 

 

namespace Namespace\ModuleName\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class Options implements ArrayInterface
{
    /**
     * @return array
     */
    public function toOptionArray()
    {
        $options = [
            0 => [
                'label' => 'Please select',
                'value' => 0
            ],
            1 => [
                'label' => 'Option 1',
                'value' => 1
            ],
            2  => [
                'label' => 'Option 2',
                'value' => 2
            ],
            3 => [
                'label' => 'Option 3',
                'value' => 3
            ],
        ];

        return $options;
    }
}

app/code/Namespace/ModuleName/view/adminhtml/web/js/form/element/options.js

 

define([
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
    'use strict';

    return select.extend({

        /**
         * On value change handler.
         *
         * @param {String} value
         */
        onUpdate: function (value) {
            console.log('Selected Value: ' + value);

            var field1 = uiRegistry.get('index = field2Depend1');
            if (field1.visibleValue == value) {
                field1.show();
            } else {
                field1.hide();
            }

            var field2 = uiRegistry.get('index = field3Depend1');
            if (field2.visibleValue == value) {
                field2.show();
            } else {
                field2.hide();
            }

            return this._super();
        },
    });
});

Kindly find below link of original answer 

Url: https://magento.stackexchange.com/questions/132020/magento-2-1-how-do-i-create-form-component-field-...

 

Hope it will help you to solve this problem then please accept as solution 

 

Re: Ui Component - display sub options when parent option is selected

@prakash786

 

Thank you very much for the code.

I have tried it and it displays field 1, but when an option's chosen it gives an error.

 

Uncaught TypeError: Cannot read property 'visibleValue' of undefined
    at UiClass.onUpdate (options.js:20)
    at UiClass.onUpdate (wrapper.js:109)
    at Function.notifySubscribers (knockout.js:1103)
    at Function.observable.valueHasMutated (knockout.js:1300)
    at observable (knockout.js:1285)
    at Object.writeValueToProperty (knockout.js:2337)
    at HTMLSelectElement.valueUpdateHandler (knockout.js:4477)
    at HTMLSelectElement.dispatch (jquery.js:5226)
    at HTMLSelectElement.elemData.handle 

Re: Ui Component - display sub options when parent option is selected

 

<field name="field1">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magebull\MultiSlider\Model\Config\Source\Options</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Parent Option</item>
            <item name="component" xsi:type="string">Magebull_MultiSlider/js/form/element/options</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="dataType" xsi:type="string">number</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="source" xsi:type="string">item</item>
            <item name="dataScope" xsi:type="string">field1</item>
            <item name="sortOrder" xsi:type="number">210</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
        </item>
    </argument>
</field>

<field name="field2Depend1">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Field 1</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">item</item>
            <item name="sortOrder" xsi:type="number">220</item>
            <item name="breakLine" xsi:type="boolean">true</item>
            <item name="visibleValue" xsi:type="string">2</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</field>
<field name="field3Depend1">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Field 2</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">item</item>
            <item name="sortOrder" xsi:type="number">230</item>
            <item name="breakLine" xsi:type="boolean">true</item>
            <item name="visibleValue" xsi:type="string">0</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</field>

Field Name

name="field1"

name="field2Depend1"

name="field3Depend1"


This soruce model files for name="field1"

Magebull\MultiSlider\Model\Config\Source\Options

Js file

Magebull_MultiSlider/js/form/element/options

 

 

define([
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
    'use strict';

    return select.extend({

        /**
         * On value change handler.
         *
         * @param {String} value
         */
        onUpdate: function (value) {
            console.log('Selected Value: ' + value);

            var field1 = uiRegistry.get('index = field2Depend1');
            if (field1.visibleValue == value) {
                field1.show();
            } else {
                field1.hide();
            }

            var field2 = uiRegistry.get('index = field3Depend1');
            if (field2.visibleValue == value) {
                field2.show();
            } else {
                field2.hide();
            }

            return this._super();
        },
    });
});

 

 var field1 = uiRegistry.get('index = field2Depend1');

var field2 = uiRegistry.get('index = field3Depend1');

 

Bold text is field name that why you got js error.

Hope it will help to solve the problem then please accept as solution and kudos.

 

 

Re: Ui Component - display sub options when parent option is selected

Thanks for your reply, but I'm still not getting it.

What exactly should I change on bold texts?

Re: Ui Component - display sub options when parent option is selected


@tvgarden wrote:

Thanks for your reply, but I'm still not getting it.

What exactly should I change on bold texts?


 

<field name="field2Depend1">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Field 1</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">item</item>
            <item name="sortOrder" xsi:type="number">220</item>
            <item name="breakLine" xsi:type="boolean">true</item>
            <item name="visibleValue" xsi:type="string">2</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</field>

 

var field1 = uiRegistry.get('index = field2Depend1');

var field2 = uiRegistry.get('index = field3Depend1');

 

After change in js please run static content and make sure your change reflect into pub js because it's working in my system.

 

Re: Ui Component - display sub options when parent option is selected

Just realised that when the field is not visible it doesn't define the field.