1

I have a lWC that passes a list to another lwc. All was fine until an exiting account added 16k records. Now, my LWC takes forever to load. From what I can find, it seems that Lazy Loading would help me. Please correct me if that is wrong. But my question is how do I implement lazy loading when the actual display of records is in a child lwc?

This is on my parent html. The child displays the records:

<c-gs-list account-id={accountId} numberrecords={numberrecords} account-name={acctname} stay-on-page={stayOnPage} filteredgroupstructures={filteredgroupstructures} onclose = {refreshHandler} pass-previous-page-down={passPreviousPageDown}></c-gs-list>

In the datatable I am supposed to add " onloadmore={loadMoreData} "

I'm just unsure of how to bridge the gap to call the loadMoreData from the child component.

Thank you!!!

1 Answer 1

1
  1. Move Data Fetching to the Child Component Instead of passing the entire list (filteredgroupstructures) from the parent, pass only the accountId and other filters. Let the child component fetch and load data in chunks.

  2. Use lightning-datatable with onloadmore In the child component (c-gs-list), use the onloadmore event to load more data when the user scrolls.

    <lightning-datatablekey-field="id"data={visibleData}columns={columns}onloadmore={handleLoadMore}enable-infinite-loadingis-loading={isLoading}>

  3. Child JS: Handle Lazy Loading

    import { LightningElement, api, track } from 'lwc'; import getRecords from '@salesforce/apex/YourApexClass.getRecords';

    export default class GsList extends LightningElement { @api accountId; @track visibleData = []; @track isLoading = false; offset = 0; limit = 50;

    connectedCallback() {
        this.loadMoreData();
    }
    
    handleLoadMore() {
        this.loadMoreData();
    }
    
    loadMoreData() {
        this.isLoading = true;
        getRecords({ accountId: this.accountId, offset: this.offset, limit: this.limit })
            .then(result => {
                this.visibleData = [...this.visibleData, ...result];
                this.offset += this.limit;
                this.isLoading = false;
            })
            .catch(error => {
                console.error(error);
                this.isLoading = false;
            });
    }
    

    }

  4. In Apex Controller

    @AuraEnabled(cacheable=true) public static List<YourObject__c> getRecords(Id accountId, Integer offset, Integer limit) { return [SELECT Id, Name FROM YourObject__c WHERE Account__c = :accountId LIMIT :limit OFFSET :offset]; }

1
  • Thanks Akash! I was using the main component to filter the records but I will just pass the filters and query on the child Commented Sep 15 at 12:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.