Change options in a dropdown field when a relate field changes

Hi All,

            Can you update how to change the drop down field when a relate field changes.

On change event we are checking the condition and doing as below:

relate field == 'xyz' then dropdown field = a,b,c as options in the drop down list

if relate field ==' mno' then dropdown field = d,e,f as options in  the drop down list.

1.We are able to get options but unable to update in the dropdown list when the relate field changes.

When we tried with below code:

this.model.fields['ds_c'].options = divSub_value_list;
this._super("_render"); 

above code it is rendering full view,with this we are getting problem with show and hide fields through formula.

2.When we are trying with particular field render with below code the drop down list is not updating.

    var renderField = this.getField('<field_name>');  
    renderField.render(); 

So how to update the dropdown list options based on relate field change.i am using version 7.6.1.0 ENT

Francesca Shiekh Ramana Raju Santhana Mehul  Bhandari Bhavesh Patel Angel Magana

 David López

Regards

Sidhu

Parents
  • Hi sidhu sidhu,

    I can show you a working example which changes the options of a dropdown list when another field changes its value.

    Let's say your module is Accounts, so you would create the following file: custom/modules/Accounts/clients/base/fields/enum/enum.js with the below content:

    ({

        extendsFrom: 'EnumField',
        initialize: function(opts){
            this._super('initialize',[opts]);
            this._initEvents();
        },

        _initEvents: function(){
            this._changeIndustry();
        },

        _changeIndustry: function() {
            this.model.on("change:account_type", this.changeIndustryHandler, this);
        },

        changeIndustryHandler: function(){
            var self = this;
            if (self.name === 'industry') {
                var newOptions = {
                    'Apparel': 'Apparel'
                };
                self.items = newOptions;
            }
        }
    })

    As you see here, I am handling the event change:account type, which triggers the function changeIndustryHandler when the value of the field account_type changes.

    There, I am changing the options (self.items) of the Dropdown.

    You should adapt this code in order to handle the event of the change of your relate field and change the options of the dropdown as desired.

    If you need further clarification do not hesitate to ask me.

    Hope it helps!

    Regards,

    David.

Reply
  • Hi sidhu sidhu,

    I can show you a working example which changes the options of a dropdown list when another field changes its value.

    Let's say your module is Accounts, so you would create the following file: custom/modules/Accounts/clients/base/fields/enum/enum.js with the below content:

    ({

        extendsFrom: 'EnumField',
        initialize: function(opts){
            this._super('initialize',[opts]);
            this._initEvents();
        },

        _initEvents: function(){
            this._changeIndustry();
        },

        _changeIndustry: function() {
            this.model.on("change:account_type", this.changeIndustryHandler, this);
        },

        changeIndustryHandler: function(){
            var self = this;
            if (self.name === 'industry') {
                var newOptions = {
                    'Apparel': 'Apparel'
                };
                self.items = newOptions;
            }
        }
    })

    As you see here, I am handling the event change:account type, which triggers the function changeIndustryHandler when the value of the field account_type changes.

    There, I am changing the options (self.items) of the Dropdown.

    You should adapt this code in order to handle the event of the change of your relate field and change the options of the dropdown as desired.

    If you need further clarification do not hesitate to ask me.

    Hope it helps!

    Regards,

    David.

Children
  • Hi David López , Mehul  Bhandari

                      We will try your suggestions and get back to you.

    Regards

    Sidhu

  • Hi David López

    When we tried with your approach we got empty dropdown options.

    But when we console and check then we are able to see opitons in that but values are not updating on the dropdown list.

    Following are the steps and code we followed:

    1. We created as you said custom/modules/Cases/clients/base/fields/DropEnum/DropEnum.js 

     ({

        extendsFrom: 'EnumField',
        initialize: function(opts){
            this._super('initialize',[opts]);
            this._initEvents();
        },

        _initEvents: function(){
            this._changeAccounts();
        },

        _changeAccounts: function() {
            this.model.on("change:account_name", this.changeDropdownHandler, this);
        },

        changeDropdownHandler: function(){
         console.log("Am from changeDropdownHandler");
            var self = this;
            console.log(self.name,"Am from changeDropdownHandler and Name");
            if (self.name === 'dropdown_sub_name_c') {
                var newOptions = {
              '':'',
                    'John': 'John',
                    'Glen':'Glen'
                };
                self.items = newOptions;
                self.render();
                console.log(self.items,"Am from changeDropdownHandler as Items");
            }
            console.log(self,"Am from changeDropdownHandler as self values...");
        }
    })

    In custom/modules/Cases/clients/base/views/record/record.php

                  3 => 
                  array (
                    'name' => 'dropdown_sub_name_c',
                    'type' => 'customDropEnum',
                    'studio' => 'visible',
                    'label' => 'LBL_DROPDOWN_SUB_NAME',
                  ),

     

    I have run Quick R&R.

    I need the option should be updated in the dropdown list when Account Name is changed.

    Regards

    Sidhu