Last active
May 12, 2020 16:40
-
-
Save tushar30/7f21b3366246dd488d1da31ace2c9c71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataTableController { | |
@AuraEnabled | |
public static DataTableWrapper initRecords(String ObjectName, String fieldNamesStr, String Orderby, String OrderDir) { | |
DataTableWrapper dtw = new DataTableWrapper(); | |
List<LabelDescriptionWrapper> labelList = new List<LabelDescriptionWrapper>(); | |
List<String> fieldSet = new List<String>(); | |
Set<String> fieldNameSet = new Set<String>(fieldNamesStr.split(',')); | |
if(Schema.getGlobalDescribe().containsKey(ObjectName) ) { | |
sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ; | |
//get all the labels for sObject fields and put them in a map, keyed to the field api name | |
Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap(); | |
Map<Schema.SObjectField,String> fieldToAPIName = new Map<Schema.SObjectField,String>(); | |
Map<String, String> apiNameToLabel = new Map<String, String>(); | |
for(String fieldName : fieldNameSet){ | |
if(fieldMap.containsKey(fieldName)) { | |
fieldSet.add(fieldName); | |
labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), fieldName, fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), true )); | |
} | |
} | |
//call method to query | |
List<sObject> sObjectRecords = getsObjectRecords(ObjectName, fieldSet, 50, '', Orderby, OrderDir); | |
dtw.ldwList = labelList; | |
dtw.sobList = sObjectRecords; | |
dtw.fieldsList = fieldSet; | |
dtw.totalCount = Database.countQuery('SELECT count() FROM '+ObjectName); | |
} | |
return dtw; | |
} | |
@AuraEnabled | |
public static List<sObject> getsObjectRecords(String ObjectName, List<String> fieldNameSet, Integer LimitSize, String recId, String Orderby, String OrderDir) { | |
OrderDir = String.isBlank(OrderDir) ? 'asc' : OrderDir; | |
String query = 'SELECT '+String.join(fieldNameSet, ',')+' FROM '+ObjectName; | |
if(String.isNotBlank(recId)) { | |
recId = String.valueOf(recId); | |
query += ' WHERE ID >: recId '; | |
} | |
query += ' ORDER BY '+Orderby+' '+OrderDir+' NULLS LAST'; | |
if(LimitSize != null && Integer.valueOf(LimitSize) > 0) { | |
LimitSize = Integer.valueOf(LimitSize); | |
query += ' Limit '+LimitSize; | |
} | |
return Database.query(query); | |
} | |
public class DataTableWrapper { | |
@AuraEnabled | |
public List<LabelDescriptionWrapper> ldwList; | |
@AuraEnabled | |
public List<sObject> sobList; | |
@AuraEnabled | |
public List<String> fieldsList; | |
@AuraEnabled | |
public Integer totalCount; | |
} | |
public class LabelDescriptionWrapper { | |
@AuraEnabled | |
public String label; | |
@AuraEnabled | |
public String fieldName; | |
@AuraEnabled | |
public String type; | |
@AuraEnabled | |
public boolean sortable; | |
public LabelDescriptionWrapper(String labelTemp, String fieldNameTemp, String typeTemp, boolean sortableTemp) { | |
label = labelTemp; | |
fieldName = fieldNameTemp; | |
type = typeTemp; | |
sortable = sortableTemp; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="DataTableController"> | |
<!-- attributes --> | |
<aura:attribute name="objectName" type="String" default="Account"/> | |
<aura:attribute name="fieldsString" type="String" default="Name,Phone,Email,Website"/> | |
<aura:attribute name="fieldsList" type="List" default="[]"/> | |
<aura:attribute name="columns" type="List" default="[]"/> | |
<aura:attribute name="data" type="List" default="[]"/> | |
<aura:attribute name="keyField" type="String" default="id"/> | |
<aura:attribute name="initialRows" type="Integer" default="5"/> | |
<aura:attribute name="selectedRowsCount" type="Integer" default="0"/> | |
<aura:attribute name="enableInfiniteLoading" type="Boolean" default="true"/> | |
<aura:attribute name="rowsToLoad" type="Integer" default="50"/> | |
<aura:attribute name="totalNumberOfRows" type="Integer" default="1000"/> | |
<aura:attribute name="loadMoreStatus" type="String" default=""/> | |
<aura:attribute name="sortedBy" type="String" default="Id"/> | |
<aura:attribute name="sortedDirection" type="String" default="ASC"/> | |
<!-- handlers--> | |
<aura:handler name="init" value="{! this }" action="{! c.init }"/> | |
<div class="slds-is-relative"> | |
<!-- toolbox --> | |
<!-- the container element determine the height of the datatable --> | |
<div style="height: 300px"> | |
<lightning:datatable | |
columns="{! v.columns }" | |
data="{! v.data }" | |
keyField="{! v.keyField }" | |
showRowNumberColumn="true" | |
sortable = "true" | |
editable = "true" | |
onsort = "{! c.updateColumnSorting }" | |
sortedBy="{!v.sortedBy}" | |
sortedDirection="{!v.sortedDirection}" | |
onrowselection="{! c.updateSelectedText }" | |
enableInfiniteLoading="{! v.enableInfiniteLoading }" | |
loadMoreOffset="{! v.loadMoreOffset }" | |
onloadmore="{! c.loadMoreData }" | |
/> | |
</div> | |
{! v.loadMoreStatus } | |
</div> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
initData: function (cmp, event, numberOfRecords) { | |
var action = cmp.get("c.initRecords"); | |
action.setParams({ | |
ObjectName : cmp.get("v.objectName"), | |
fieldNamesStr : cmp.get("v.fieldsString"), | |
Orderby : cmp.get("v.sortedBy"), | |
OrderDir : cmp.get("v.sortedDirection") | |
}); | |
action.setCallback(this, function(response) { | |
var state = response.getState(); | |
if (state === "SUCCESS") { | |
console.log(response.getReturnValue().ldwList); | |
cmp.set("v.columns", response.getReturnValue().ldwList); | |
cmp.set("v.data", response.getReturnValue().sobList); | |
cmp.set("v.fieldsList", response.getReturnValue().fieldsList); | |
cmp.set('v.loadMoreStatus', ''); | |
event.getSource().set("v.isLoading", false); | |
} | |
else if (state === "ERROR") { | |
var errors = response.getError(); | |
if (errors) { | |
if (errors[0] && errors[0].message) { | |
console.log("Error message: " + | |
errors[0].message); | |
} | |
} else { | |
console.log("Unknown error"); | |
} | |
} | |
}); | |
$A.enqueueAction(action); | |
}, | |
fetchData: function (cmp, event, numberOfRecords) { | |
//var dataPromise; | |
var data = cmp.get("v.data"); | |
var dataSize = cmp.get("v.data").length; | |
var lastId = data[dataSize - 1].Id; | |
console.log('--lastId----'+lastId); | |
var action = cmp.get("c.getsObjectRecords"); | |
action.setParams({ | |
ObjectName : cmp.get("v.objectName"), | |
fieldNameSet : cmp.get("v.fieldsList"), | |
LimitSize : 50, | |
recId : lastId, | |
Orderby : cmp.get("v.sortedBy"), | |
OrderDir : cmp.set("v.sortedDirection") | |
}); | |
action.setCallback(this, function(response) { | |
var state = response.getState(); | |
if (state === "SUCCESS") { | |
if (cmp.get('v.data').length >= cmp.get('v.totalNumberOfRows')) { | |
cmp.set('v.enableInfiniteLoading', false); | |
cmp.set('v.loadMoreStatus', 'No more data to load'); | |
} else { | |
var currentData = cmp.get('v.data'); | |
var newData = currentData.concat(response.getReturnValue()); | |
cmp.set('v.data', newData); | |
cmp.set('v.loadMoreStatus', ''); | |
} | |
event.getSource().set("v.isLoading", false); | |
} | |
else if (state === "ERROR") { | |
var errors = response.getError(); | |
if (errors) { | |
if (errors[0] && errors[0].message) { | |
console.log("Error message: " + | |
errors[0].message); | |
} | |
} else { | |
console.log("Unknown error"); | |
} | |
} | |
}); | |
$A.enqueueAction(action); | |
}, | |
sortData: function (cmp, fieldName, sortDirection) { | |
var data = cmp.get("v.data"); | |
var reverse = sortDirection !== 'asc'; | |
//sorts the rows based on the column header that's clicked | |
data.sort(this.sortBy(fieldName, reverse)) | |
cmp.set("v.data", data); | |
}, | |
sortBy: function (field, reverse, primer) { | |
var key = primer ? | |
function(x) {return primer(x[field])} : | |
function(x) {return x[field]}; | |
//checks if the two rows should switch places | |
reverse = !reverse ? 1 : -1; | |
return function (a, b) { | |
return a = key(a), b = key(b), reverse * ((a > b) - (b > a)); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
init: function (cmp, event, helper) { | |
console.log( cmp.get("v.data").length ); | |
helper.initData(cmp, event, cmp.get("v.data").length); | |
}, | |
updateSelectedText: function (cmp, event) { | |
var selectedRows = event.getParam('selectedRows'); | |
cmp.set('v.selectedRowsCount', selectedRows.length); | |
}, | |
resetRows: function (cmp, event, helper) { | |
cmp.set('v.data', []); | |
helper.initData(cmp, event, cmp.get("v.data").length); | |
}, | |
loadMoreData: function (cmp, event, helper) { | |
var rowsToLoad = cmp.get('v.rowsToLoad'); | |
event.getSource().set("v.isLoading", true); | |
cmp.set('v.loadMoreStatus', 'Loading'); | |
helper.fetchData(cmp, event, cmp.get("v.data").length); | |
}, | |
updateColumnSorting: function (cmp, event, helper) { | |
var fieldName = event.getParam('fieldName'); | |
var sortDirection = event.getParam('sortDirection'); | |
cmp.set("v.sortedBy", fieldName); | |
cmp.set("v.sortedDirection", sortDirection); | |
cmp.set('v.data', []); | |
event.getSource().set("v.isLoading", true); | |
cmp.set('v.loadMoreStatus', 'Loading'); | |
helper.initData(cmp, event, cmp.get("v.data").length); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment