You need to sign in to do that
Don't have an account?
Chhavi Jain 15
getting below error even we have records.
This page has an error. You might just need to refresh it. Action failed: c:csvSample$controller$loadContactList [component is not defined] Failing descriptor: {c:csvSample$controller$loadContactList}
componenet, event) change this to onLoad : function(component, event)Naveen
Team codengine.in
All Answers
Naveen
<aura:handler name = "init" value="{!this}" action="{!c.loadContactList}"/>
<aura:attribute name="ListOfContact" type="contact[]" default="{'sobjectType' : 'contact'}"/>
<div class="slds-m-around--xx-large">
<button class="slds-button slds-button--brand" onclick="{!c.downloadCsv}">Download As CSV</button>
<br/><br/>
<table class="slds-table slds-table--bordered slds-table--cell-buffer">
<thead>
<tr class="slds-text-title--caps">
<th class="slds-is-sortable slds-text-title--caps" scope="col">
<span class="slds-truncate" title="Name">First Name</span>
</th>
<th class="slds-is-sortable slds-text-title--caps" scope="col">
<span class="slds-truncate" title="Name">Last Name</span>
</th>
<th class="slds-is-sortable slds-text-title--caps" scope="col">
<span class="slds-truncate" title="Name">Department</span>
</th>
<th class="slds-is-sortable slds-text-title--caps" scope="col">
<span class="slds-truncate" title="Name">Mobile Phone</span>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.ListOfContact}" var="con">
<tr>
<th scope="row">
<div class="slds-truncate" title="{!con.FirstName}">{!con.FirstName}</div>
</th>
<th scope="row">
<div class="slds-truncate" title="{!con.Firstname}">{!con.LastName}</div>
</th>
<th scope="row">
<div class="slds-truncate" title="{!con.Firstname}">{!con.Department}</div>
</th>
<th scope="row">
<div class="slds-truncate" title="{!con.Firstname}">{!con.MobilePhone}</div>
</th>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
</aura:component>
===========================================================================================================
public class csvDownloadCtrl {
@AuraEnabled
public static list<contact> fetchContact()
{
List<contact> returnConList = new List<contact>();
for(contact con : [Select firstName, lastName, Department, Mobilephone from contact])
{
returnConList.add(con);
System.debug('====' +returnConList);
}
return returnConList;
}
============================================================================================================
({
loadContactList : function(component, event, helper) {
console.log("loading");
helper.onLoad(component, event);
console.log("successfully loading");
},
downloadCsv : function(component, event, helper)
{
var stockData = component.get('v.ListOfContact');
var csv = helper.convertArrayOfObjectsToCSV(component, stockData);
if(csv == null)
{
return;
}
var hiddenElement =document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' +encodeURI(csv);
hiddenElement.target = '_self';
hiddenElement.download = 'ExportData.csv';
document.body.appendChild(hiddenElement);
hiddenElement.click();
}
})
==============================================================================================================
({
onLoad : function(componenet, event)
{
var action = component.get('c.fetchContact');
action.setCallback(this, function(response)
{
var state = response.getState();
if(state == "SUCCESS")
{
component.set('v.ListOfContact', response.getReturnValue());
console.log('===');
}
});
$A.enqueueAction(action);
},
convertArrayOfObjectsToCSV : function(component, objectRecords)
{
var csvStringResult, counter, keys, columnDivider, lineDivider;
if(objectRecords == null || !objectRecords.length)
{
return null;
}
columnDivider = ',';
lineDivider = '\n';
keys = ['FirstName', 'LastName', 'Department', 'MobilePhone'];
csvStringResult = '';
csvStringResult += keys.join(columnDivider);
csvStringResult += keys.join(lineDivider);
for(var i = 0; i<objectRecords.length; i++)
{
counter = 0;
for(var sTempKey in keys)
{
var skey =keys[sTempKey];
if(counter > 0)
{
csvStringResult += columnDivider;
}
csvStringResult += '"'+objectRecords[i][skey]+'"';
counter++;
}
csvStringResult += lineDivider;
}
return csvStringResult;
},
})
componenet, event) change this to onLoad : function(component, event)Naveen
Team codengine.in
Happy coding!
Team codengine.in
Naveen