Setting Dependent Custom Lightning Picklist Level2 and Level3 then resetting the Level2 at Lightning component but Level2 Cached Data is Getting Saved

后端 未结 1 920
面向向阳花
面向向阳花 2021-01-26 09:09

Step 1

In a Salesforce Lightning component I have a scenario with three levels of dependent picklists, first I am setting values for picklist Level1, and

相关标签:
1条回答
  • 2021-01-26 09:49

    This got fixed because I not refreshing the Level 3 picklist when the Level 1 picklist was getting changed,so added an onchange at Level3

    Also copied getSelectedValue to Helper at getLvl1 function using helper.getSelectedValue(component,event,helper); (It is to be remembered one good use of helper class is this when we need some code or method to be repeatedly called,so its better to put that function in helper ) and called again at component also changed the <ui:inputSelect with new aura components like <lightning:select

    Here is component

                    <aura:component controller="PickListHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
                        <!-- Actions-->
                        <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
                      
                        <!-- variable-->
                        <aura:attribute name="lstLevel1" type="String[]" />
                         <aura:attribute name="lstLevel2" type="String[]"  />
                          <aura:attribute name="lstL3"  type="String[]"  />
                            <aura:attribute name="firstlevel1selected" type="String" default="" />
                         <aura:attribute name="secondlevelselected" type="String" default="" />
                          <aura:attribute name="thirdlevelselected"  type="String" default="" />
                        
                           <div class="slds-container--center slds-container--small slds-m-top--small">
                            <div class="slds-form--stacked">
                                 
                                <lightning:select name="parentPicklist" label="Level 1" aura:id="ddLevel1" onchange="{!c.getLvl1}">
                                    <option value="">--- None ---</option>
                                    <aura:iteration items="{!v.lstLevel1}" var="value">
                                        <option value="{!value}">{!value}</option>
                                    </aura:iteration>
                                </lightning:select>
                                 
                                <lightning:select name="Level2Picklist" label="Level 2" aura:id="ddLevel2"  onchange="{!c.getSelectedValue}" >
                                    <option value="">--- None ---</option>
                                    <aura:iteration items="{!v.lstLevel2}" var="value">
                                        <option value="{!value}">{!value}</option>
                                    </aura:iteration>
                                </lightning:select>
                                
                                 <lightning:select name="Level3Picklist" label="Level 3" aura:id="ddLevel3" onchange="{!c.getlevel3}"  >
                                    <option value="">--- None ---</option>
                                    <aura:iteration items="{!v.lstL3}" var="value">
                                        <option value="{!value}">{!value}</option>
                                    </aura:iteration>
                                </lightning:select>
                                 
                            </div>        
                        </div>
                       <lightning:button variant="brand" label="Save" onclick="{!c.onConfirm}" />
                    </aura:component>
    

    Here is controller js

                    doInit : function(component, event, helper) {
                        var action = component.get("c.getLevel1");
                        action.setCallback(this, function(e) {
                            if(e.getState()=='SUCCESS'){
                                var result=e.getReturnValue();
                                component.set("v.lstLevel1",result);
                            }
                        });
                        $A.enqueueAction(action);
                    },    
                    getLvl1:function(component, event, helper){
                       
                        var picklist=component.find('ddLevel1');
                        var picklistvalue=picklist.get('v.value');
                        component.set("v.firstlevel1selected",picklistvalue);
                        var action = component.get("c.getLevel2");
                        action.setParams({  'strName' : picklistvalue  });
                        action.setCallback(this, function(e) {
                            if(e.getState()=='SUCCESS'){
                                var result=e.getReturnValue();
                                component.set("v.lstLevel2",result);
                                helper.getSelectedValue(component,event,helper);
                            }
                        });
                        $A.enqueueAction(action);
                            },
                    getSelectedValue:function(component, event, helper){
                        var picklist=component.find('ddLevel1');
                       
                        var picklistvalue=picklist.get('v.value');
                         
                        var picklistdep=component.find('ddLevel2');
                        
                        var picklistvaluedep2=picklistdep.get('v.value');
                        component.set("v.secondlevelselected",picklistvaluedep2);
                        var action = component.get("c.getLevel3");
                        
                        action.setParams({  'strName1' : picklistvalue,
                                         'strName2' : picklistvaluedep2});//
                        action.setCallback(this, function(e) {
                            if(e.getState()=='SUCCESS'){
                                var result=e.getReturnValue();
                                component.set("v.lstL3",result);
                            }
                        });
                        $A.enqueueAction(action);
                    },
                    getlevel3:function(component, event, helper){
                        var picklist=component.find('ddLevel3');
                       
                        var picklistvalue=picklist.get('v.value');
                         component.set("v.thirdlevelselected",picklistvalue);
                        
                    },
                    onConfirm:function(component, event, helper){
                        var picklist=component.find('ddLevel1');
                        var picklistvalue=picklist.get('v.value');
                        var picklistdep=component.find('ddLevel2');
                        var picklistvaluedep2=picklistdep.get('v.value');
                       
                        var picklistdep3=component.find('ddLevel3');
                        var picklistvaluedep3=picklistdep3.get('v.value');
                        console.log(component.get('v.firstlevel1selected'));
                         console.log(component.get('v.secondlevelselected'));
                         console.log(component.get('v.thirdlevelselected'));
                        
                        var action = component.get("c.savecasetype");
                        
                        action.setParams({  'level1' : picklistvalue,
                                          'level2' : picklistvaluedep2,
                                          'level3' : picklistvaluedep3,
                                          'id' : component.get("v.recordId")});
                                          
                        
                        var toastEvent = $A.get("e.force:showToast");
                        action.setCallback(this, function(e) {
                            if(e.getState()=='SUCCESS'){
                                var result=e.getReturnValue();
                                if(result==='successfull'){
                                    toastEvent.setParams({
                                        "title": "Success!",
                                        "message": "The record has been inserted  successfully."
                                    });
                                    toastEvent.fire();
                                }else{
                                    toastEvent.setParams({
                                        "title": "Error",
                                        "message": "The record has not been inserted  successfully."
                                    });
                                    toastEvent.fire();
                                }
                            }
                        });
                        $A.enqueueAction(action);
                       
                    },
                       
                       
                })              
    

    Here is helper class

                ({
                     getSelectedValue:function(component, event, helper){
                        var picklist=component.find('ddLevel1');
                       
                        var picklistvalue=picklist.get('v.value');
                         
                        var picklistdep=component.find('ddLevel2');
                        
                        var picklistvaluedep2=picklistdep.get('v.value');
                        component.set("v.secondlevelselected",picklistvaluedep2);
                        var action = component.get("c.getLevel3");
                        
                        action.setParams({  'strName1' : picklistvalue,
                                         'strName2' : picklistvaluedep2});//
                        action.setCallback(this, function(e) {
                            if(e.getState()=='SUCCESS'){
                                var result=e.getReturnValue();
                                component.set("v.lstL3",result);
                            }
                        });
                        $A.enqueueAction(action);
                    },
                    getlevel3:function(component, event, helper){
                        var picklist=component.find('ddLevel3');
                       
                        var picklistvalue=picklist.get('v.value');
                         component.set("v.thirdlevelselected",picklistvalue);
                        
                    }
                })  
    
    0 讨论(0)
提交回复
热议问题