3

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?

2

Generic 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

  1. Gets data from database automatically. Can use relationship fields also.
  2. Sort functionality (across pages)
  3. Pagination - first, previous, next and last pages
  4. Persistant selection of records across pages. getSelectedRows public method to get selected data.
  5. All events of lightning-datatable plus event while loading data
  6. Cacheable data
  7. Sosl search
  8. Dynamically change data filters

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:

enter image description here


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 }
                ]
            }
        });
    },
})
| improve this answer | |
  • 1
    +1 Thanks for the guide :) All these code are quite useful. I fear it will get lost in the question. Do you have a blog which I can follow? – Pranay Jaiswal Aug 2 '19 at 15:40
  • I have not yet put it in blog. Will create detailed use cases and put it in blog by next week. And yes you are right, I think my question wording might not be correct, or else this comp would be quite useful. – salesforce-sas Aug 2 '19 at 15:51
  • @salesforce-sas can you share the link of your blog? – dibo Oct 30 '19 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.