The below example explain about the way of loading custom spinning or processing image in Lightning Component.
Step 1:
Create a Apex Class with the below code snippet,
Step 2:
Step 1:
Create a Apex Class with the below code snippet,
- public class OpportunityController {
- @AuraEnabled
- public static List<Opportunity> getOpportunities() {
- List<Opportunity> opportunities =
- [SELECT Id, Name, CloseDate FROM Opportunity LIMIT 1000];
- return opportunities;
- }
- }
Step 2:
Create a lightning component with the below code snippet. Controller attribute in the aura:component refers the apex class.
- <aura:component controller="OpportunityController">
- <aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
- <aura:handler event="aura:doneWaiting" action="{!c.hideSpinner}"/>
- <aura:attribute name="opportunities" type="Opportunity[]"/>
- <aura:attribute name="showSpinnerImage" type="Boolean"/>
- <center>
- <ui:button label="Get Opportunities" press="{!c.getOpps}"/> <br/> <br/>
- <aura:renderIf isTrue="{!v.showSpinnerImage}">
- <img src="https://media.giphy.com/media/npCmk0VCFHyUM/giphy.gif"/>
- <aura:set attribute="else">
- <aura:iteration var="opportunity" items="{!v.opportunities}">
- <p>{!opportunity.Name} : {!opportunity.CloseDate}</p> <br/>
- </aura:iteration>
- </aura:set>
- </aura:renderIf>
- </center>
- </aura:component>
Custom spinning image dynamically rendered using aura:renderIf tag.
Step 3:
Double click on CONTROLLER from the side bar in lightning component editor and paste the below java script snippet.
- ({
- showSpinner : function (component, event, helper) {
- component.set("v.showSpinnerImage", true);
- },
- hideSpinner : function (component, event, helper) {
- component.set("v.showSpinnerImage", false);
- },
- getOpps: function(cmp){
- var action = cmp.get("c.getOpportunities");
- action.setCallback(this, function(response){
- var state = response.getState();
- if (state == "SUCCESS") {
- cmp.set("v.opportunities", response.getReturnValue());
- }
- });
- $A.enqueueAction(action);
- }
- })
Output:
Great..! keep it up the good work...!w3websurfmyindia
ReplyDelete