About

Edit photo

Thursday, November 9, 2017

Retrieving lightning component tips


How to get Lightning Button label?
<lightning:button label="Get Label" onclick="{!c.getLabel}" aura:id="button1" />  
<aura:attribute name="buttonLabel" aura:id="btn1" type="String"/>

 var BtnLabelWay1 = event.getSource().get("v.label"); -- Used when any Action event occurs, ex: onClick  
 var BtnLabelWay2 = component.find("button1").get("v.label");          -- Can use when auraID exist  

Get and Set Attribute Value
 var att1 = component.get("v.buttonLabel"); -- Getting attribute   
 var att1 = component.find("btn1"); -- Getting attribute using aura:ID  
 component.set(att1,"this is the value for the buttonLabel"); -- Setting attribute with value   

Attribute is empty or not?
 var isEmpty = $A.util.isEmpty(cmp.get("v.label"));  


Wednesday, November 8, 2017

How to get Salesforce Lightning event label to controller?


The below code is a sample component, is having one attribute for message, and has 2 lightning buttons.
Wants to display the button label on Lighting component.

 <aura:component>  
   <aura:attribute name="message" type="String"/>  
   <p>Message of the day: {!v.message}</p>  
   <div>  
     <lightning:button label="You look nice today."  
       onclick="{!c.handleClick}"/>  
     <lightning:button label="Today is going to be a great day!"  
       onclick="{!c.handleClick}"/>  
   </div>  
 </aura:component>  

Controller:
 ({  
   handleClick: function(component, event, helper) {  
     var btnClicked = event.getSource();     // the button  
     var btnMessage = btnClicked.get("v.label"); // the button's label  
     component.set("v.message", btnMessage);   // update our message  
   }  
 })  

Component Displays:


The below code gets Button
 var btnClicked = event.getSource();     // the button  

The below code gets Button label
 var btnMessage = btnClicked.get("v.label"); // the button's label  

The below code updates the attribute named "message"
 component.set("v.message", btnMessage);   // update our message  


Source: trailhead.salesforce.com