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

ε祈祈猫儿з 提交于 2020-11-29 21:07:32

问题


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 Level2, then the user selects the dependent picklist value in Level3

Step 2

When I remove the selection at Level2, automatically Level3 selection also gets erased:

Step3

Click Save

Here is problem, that value selected in Step 1 is getting saved rather than the reset value in step 2.

I have added code to reset values at ui:inputSelectOption but it seems the reset values from lightning components are not getting reset and saved rather cached values are getting saved after I click save.

Please help in fixing the code such that lightning component doesn't saves the cached values rather it saves the current lightning component values.

Here is the component code:

<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}" />
  <aura:handler name="change" value="{!v.pageReference}"
          action="{!c.onPageReferenceChanged}" />
  <!-- variable-->
  <aura:attribute name="lstLevel1" type="String[]" />
    <aura:attribute name="lstLevel2" type="String[]" />
    <aura:attribute name="lstL3" type="String[]" />
  <span> Level 1</span>
  <ui:inputSelect aura:id="ddLevel1" change="{!c.getLvl1}">
    <ui:inputSelectOption label="-Select-" value="true"/>        
    <aura:iteration items="{!v.lstLevel1}" var="value">          
      <ui:inputSelectOption label="{!value}" text="{!value}" />
    </aura:iteration>
  </ui:inputSelect>
  <span>Level 2</span>
  <ui:inputSelect aura:id="ddLevel2"  change="{!c.getSelectedValue}">
      <ui:inputSelectOption label="-Select-" value="{!v.clrlv1}"/>        
    <aura:iteration items="{!v.lstLevel2}" var="value">          
      <ui:inputSelectOption label="{!value}" text="{!value}" />
    </aura:iteration>
  </ui:inputSelect>
    <span>Level 3</span>
  <ui:inputSelect aura:id="ddLevel3" >
    <ui:inputSelectOption label="-Select-" value="{!v.clrlv2}"/>        
    <aura:iteration items="{!v.lstL3}" var="value">          
      <ui:inputSelectOption label="{!value}" text="{!value}" />
    </aura:iteration>
  </ui:inputSelect>
    <lightning:button variant="brand" label="Save" onclick="{!c.onConfirm}" />
</aura:component>

here is contoller code

({
  reset1 : function(component, event, helper) {
      component.set("v.clrlvl", "");
    },
  reset2 : function(component, event, helper) {
      component.set("v.clrlv2", "");
    },onPageReferenceChanged: function(cmp, event, helper) {
      $A.get('e.force:refreshView').fire();
    },
  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');
      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);
        }
      });
      $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');
      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);
    },
    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');
      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);
        
    }
})

回答1:


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


来源:https://stackoverflow.com/questions/64868673/setting-dependent-custom-lightning-picklist-level2-and-level3-then-resetting-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!