Is it possible to fetch records automatically from database just by mentioning columns?
Also can we use relationship fields to get data without any processing?
For example CreatedBy.Name
?
Other features like sorting, pagination etc?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityGeneric component which has following features: (This can be used in both AURA and LWC parent component)
You can find the component here: https://github.com/sasank-sfdcdev/public/tree/master/src/lwc/datatable
The simplicity of this is such that, you can just put tag, and rest of the things is done by it. Example HTML:
<template>
<c-datatable config={config}>
</c-datatable>
</template>
and its JS:
import { LightningElement, track } from 'lwc';
export default class PocTable extends LightningElement {
config = {
objectName: "Account",
tableConfig: {
columns: [
{ api: 'Name', label: 'Name', fieldName: 'Name', sortable: true },
{ api: 'CreatedDate', label: 'Created On', fieldName: 'CreatedDate', type: 'date', sortable: true },
{ api: 'CreatedBy.Name', label: 'Created By', fieldName: 'CreatedByName', sortable: true }
]
}
};
}
NOTE: You need add api
property in columns
which will be used to fetch data from database.
Here is how it will render:
Here is the sample of AURA component:
<aura:component implements="lightning:isUrlAddressable,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access = "GLOBAL">
<aura:attribute name="config" type="Map" default="{}" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<c:datatable config="{!v.config}" />
</aura:component>
and controller.js
,
({
doInit : function(component, event, helper) {
component.set("v.config", {
objectName: "Account",
tableConfig: {
columns: [
{ api: 'Name', label: 'Name', fieldName: 'Name', sortable: true },
{ api: 'CreatedDate', label: 'Created On', fieldName: 'CreatedDate', type: 'date', sortable: true },
{ api: 'CreatedBy.Name', label: 'Created By', fieldName: 'CreatedByName', sortable: true }
]
}
});
},
})