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

后端 未结 1 921
面向向阳花
面向向阳花 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 with new aura components like

    Here is 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)
提交回复
热议问题