1

here i get the layer id in drop down now i have get layer type in second field in based on layer is select

enter image description here

can any one help me how can i do that.i try

    $fieldset->addField(
        'layer_id',
        'select',
        [
            'name' => 'layer_id',
            'label' => __('Layer Name'),
            'title' => __('Layer Id'),
            'required' => true,
            'values'=> $layer_data,
            'disabled' => $isElementDisabled
        ]
    );

     $fieldset->addField(
        'layer_type',
        'text',
        [
            'name' => 'layer_type',
            'label' => __('Layer Type'),
            'title' => __('Layer Type'),
            'readonly' => true,
            'disabled' => $isElementDisabled
        ]
    );
3
  • you can do this by adding custom component on your layer type field.. you only want this of page load? – Waqar Ali Jul 18 '20 at 5:34
  • can you please guide some code because i have no idea of that. – Jay Parmar Jul 19 '20 at 4:59
  • 1
    okay sure just wait.. – Waqar Ali Jul 19 '20 at 5:45
0

i Don't have worked with Block form But I worked on the same thing in UI component Hope Adding Custom component will work same in Block Like this

  • List item

Feilds:

$fieldset->addField(
    'layer_id',
    'select',
    [
        'name' => 'layer_id',
        'label' => __('Layer Name'),
        'title' => __('Layer Id'),
        'component' => 'Vendor_Module/js/form/element/containers'
        'required' => true,
        'values'=> $layer_data,
        'disabled' => $isElementDisabled
    ]
);

 $fieldset->addField(
    'layer_type',
    'text',
    [
        'name' => 'layer_type',
        'label' => __('Layer Type'),
        'title' => __('Layer Type'),
        'component' => 'Vendor_Module/js/form/element/places'
        'readonly' => true,
        'disabled' => $isElementDisabled
    ]
);

in your Vendor_Module/js/form/element/containers

This code is to change the value in Feild 2 when user change value of Feild 1

define([
    'jquery',
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select']
 , function ($,__,uiRegistry, select, modal) {
    'use strict';
   
    return select.extend({
      onUpdate: function () {
          var elementName = this.name;
          var value = this.value();
          this._super();
          return this.setDependentValue(elementName,value)
      },
      setDependentValue: function (elementName,value){
            var values = {};

            var value_for_1 = 'Your Value'
            var value_for_2 = 'your Value'
            var feild= uiRegistry.get('inputName = feildset_name[feild_2]');
            if(value==1){
              feild.setValue(value_for_1);
            }else if(value==2){
              feild.setValue(value_for_2);
            }else{
              values  = empty;
            }
            return this;
      }
     
    });
});

in your Vendor_Module/js/form/element/places

define([
    'jquery',
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
], function ($,_, uiRegistry, select) {
    'use strict';
    return select.extend({
        initialize: function () {
            this._super();
            
        },
        setValueDynamic: function () {
            var values = {};
            var elementName = this.customName;
            var value_for_1 = 'Your Value'
            var value_for_2 = 'your Value'
            var feild= uiRegistry.get('inputName = feildset_name[feild_1]');
            if(feild.value()==1){
              this.setValue(value_for_1);
            }else if(feild.value()==2){
              this.setValue(value_for_2);
            }else{
              this.setValue('');
            }
            
        },
        setInitialValue:function () {
            this.setValueDynamic();
            return this._super();
        }
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.