You need to sign in to do that
Don't have an account?
Aleksandar Aleksandar
Accessing Apex properties in Lightning Components
Hi all,
I am trying to build lightning components and I want to access the properties of an Apex controller in the components to display certain data. When try to display the values like in Visualforce (with added @AuraEnabled), it does not work. When I place the properties inside an inner class and create a method that has a return type as that inner class, then when I return a list of that class I can see the data. Bellow is the code which displays data, but I want to use it without an inner class and extra method to get that data. If anybody can help please let me know.
Thanks!
Apex:
public with sharing class EmployeeAvailability {
JavaScript controller:
getFixedTable : function(cmp, event, helper) {
//debugger;
var table = cmp.get("c.getEmployeesDailyStatus");
table.setCallback(this,function(response){
var state = response.getState();
if (state === "SUCCESS") {
cmp.set("v.tabTable",response.getReturnValue());
} else if (state === "ERROR") {
alert('Error : ' + JSON.stringify(errors));
}
});
$A.enqueueAction(table);
}
Lightning Component:
<aura:handler name="init" value="{!this}" action="{!c.getFixedTable}"/>
<aura:attribute name="tabTable" type="EmployeeAvailabilityLightning"/>
<table id="fixedTab">
<thead>
<tr>
<th>Total employees today : {!v.tabTable.totalEmployeesToday}</th>
<th>Budget this month : {!v.tabTable.budgetThisMonth}</th>
<th>Average Margin : {!v.tabTable.averageMargin} %</th>
<th>Available next 60 days : {!v.tabTable.AvailableSixty.size}</th>
<th>Employee hours available today : {!v.tabTable.aetPerc} %</th>
<th>Illness Today: {!v.tabTable.illnessToday}</th>
</tr>
</thead>
</table>
I am trying to build lightning components and I want to access the properties of an Apex controller in the components to display certain data. When try to display the values like in Visualforce (with added @AuraEnabled), it does not work. When I place the properties inside an inner class and create a method that has a return type as that inner class, then when I return a list of that class I can see the data. Bellow is the code which displays data, but I want to use it without an inner class and extra method to get that data. If anybody can help please let me know.
Thanks!
Apex:
public with sharing class EmployeeAvailability {
public static empAvailableInnerclass innerCList;
public class empAvailableInnerClass {
@AuraEnabled
public Decimal aetPerc {get;set;}
@AuraEnabled
public Integer totalEmployeesToday {get;set;}
@AuraEnabled
public Decimal budgetThisMonth {get;set;}
@AuraEnabled
public Decimal averageMargin {get;set;}
@AuraEnabled
public Object averageRate {get;set;}
@AuraEnabled
public Integer illnessToday {get;set;}
@AuraEnabled
public List<availableNextSixty> AvailableSixty{get;set;}
@AuraEnabled
public Date DateAfterTwoMonths {get;set;}
public empAvailableInnerclass() {
DateAfterTwoMonths=Date.today().addmonths(2);
}
@AuraEnabled
public Decimal aetPerc {get;set;}
@AuraEnabled
public Integer totalEmployeesToday {get;set;}
@AuraEnabled
public Decimal budgetThisMonth {get;set;}
@AuraEnabled
public Decimal averageMargin {get;set;}
@AuraEnabled
public Object averageRate {get;set;}
@AuraEnabled
public Integer illnessToday {get;set;}
@AuraEnabled
public List<availableNextSixty> AvailableSixty{get;set;}
@AuraEnabled
public Date DateAfterTwoMonths {get;set;}
public empAvailableInnerclass() {
DateAfterTwoMonths=Date.today().addmonths(2);
}
}
@AuraEnabled
public static empAvailableInnerclass getEmployeesDailyStatus(){
runRetrieve();
return innerCList;
}
}@AuraEnabled
public static empAvailableInnerclass getEmployeesDailyStatus(){
runRetrieve();
return innerCList;
}
JavaScript controller:
getFixedTable : function(cmp, event, helper) {
//debugger;
var table = cmp.get("c.getEmployeesDailyStatus");
table.setCallback(this,function(response){
var state = response.getState();
if (state === "SUCCESS") {
cmp.set("v.tabTable",response.getReturnValue());
} else if (state === "ERROR") {
alert('Error : ' + JSON.stringify(errors));
}
});
$A.enqueueAction(table);
}
Lightning Component:
<aura:handler name="init" value="{!this}" action="{!c.getFixedTable}"/>
<aura:attribute name="tabTable" type="EmployeeAvailabilityLightning"/>
<table id="fixedTab">
<thead>
<tr>
<th>Total employees today : {!v.tabTable.totalEmployeesToday}</th>
<th>Budget this month : {!v.tabTable.budgetThisMonth}</th>
<th>Average Margin : {!v.tabTable.averageMargin} %</th>
<th>Available next 60 days : {!v.tabTable.AvailableSixty.size}</th>
<th>Employee hours available today : {!v.tabTable.aetPerc} %</th>
<th>Illness Today: {!v.tabTable.illnessToday}</th>
</tr>
</thead>
</table>
May be what you can try is, create a new method in apex controller that returns instance of a wrapper apex class which ecompases all the data you need in individual variables.
http://www.sfdcmonkey.com/2016/11/17/access-apex-class-property-lightning-component/