Skip to content

Instantly share code, notes, and snippets.

@sumugapadman
Last active April 1, 2019 23:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sumugapadman/b31488774df70e50c9f8bf64d3c9ac33 to your computer and use it in GitHub Desktop.
Save sumugapadman/b31488774df70e50c9f8bf64d3c9ac33 to your computer and use it in GitHub Desktop.
public class Accountcontroller {
@AuraEnabled
public static List<Account> getAccounts() {
return [Select Id, Name, Industry, Phone from Account limit 10];
}
}
<aura:component controller="Accountcontroller" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="mydata" type="List"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:attribute name="selectedRows" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:datatable aura:id="mytbl" data="{!v.mydata}"
columns="{!v.mycolumns}"
keyField="Id"
selectedRows="{!v.selectedRows}"
onrowaction="{!c.handleAction}"
/>
</aura:component>
({
doInit : function(component, event, helper) {
component.set('v.mycolumns', [
{label: 'Account Name', fieldName: 'Name', type: 'text'},
{label: 'Phone', fieldName: 'Phone', type: 'phone'},
{label: 'Industry', fieldName: 'Industry', type: 'text'}
]);
var action = component.get("c.getAccounts");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
console.log(response.getReturnValue());
component.set('v.mydata', response.getReturnValue());
// trying to set the selected rows
var selected_rows = helper.getSomeRows(response.getReturnValue(), 3);
var selected_rows_mod = ["00161000002K6BbAAK", "00161000002K6BcAAK", "00161000002K6BdAAK"];
component.set("v.selectedRows",selected_rows_mod);
}
});
$A.enqueueAction(action);
}
})
({
getSomeRows : function(objs, cnt) {
var selected_records = [];
for(var i=0; i<cnt; i++){
selected_records.push(objs[i].Id);
}
return selected_records;
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment