TransWikia.com

How to get second field value from data base based in first field id in magento2 admin block form?

Magento Asked by Jay Parmar on October 1, 2021

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
        ]
    );

One Answer

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();
        }
    });
});

Answered by Real Magento Man on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP