4

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?

1 Answer 1

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 }
                ]
            }
        });
    },
})
4
  • 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? Aug 2, 2019 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. Aug 2, 2019 at 15:51
  • @salesforce-sas can you share the link of your blog?
    – dibocor
    Oct 30, 2019 at 16:25
  • @salesforce-sas Hi,I quoted the plug-in you provided and put it in the LWC program I wrote, but I am now facing a problem. After the first query, everything is normal, but when I query the second time, how to reload your components ? And pass the data of the second query to the component you wrote?
    – Zhang Ou
    Aug 25, 2020 at 17:13

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.