I have a lightning:datatable which I need to load data when the user scrolls. I found the onloadmore event is fired when the user clicks on a table row and not when the user scrolls on the table.
Even more, as soon as onloadmore is triggered, it keeps calling until loading all the records in the database.
Can someone help me to understand why is doing this and a workaround? Any help would be greatly appreciated.
To check how is called loadMoreData function open a inspector and check the console after clicking on a row.
Here is an example:
testDataTable.cmp
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="ContactController">
<aura:attribute name="data" type="Object"/>
<aura:attribute name="columns" type="List"/>
<aura:attribute name="rowsToLoad" type="Integer" default="10"/>
<aura:attribute name="enableInfiniteLoading" type="Boolean" default="false"/>
<aura:attribute name="loadMoreOffset" type="Integer" default="5"/>
<aura:attribute name="loadMoreStatus" type="String" default=""/>
<!-- handlers-->
<aura:handler name="init" value="{! this }" action="{! c.doInit }"/>
<lightning:card title="testDataTableCard">
<lightning:datatable
keyField="Id"
data="{! v.data }"
columns="{! v.columns }"
enableInfiniteLoading="{! v.enableInfiniteLoading }"
onloadmore="{! c.loadMoreData }"/>
</lightning:card>
</aura:component>
testDataTableController.js
({
doInit : function(component, event, helper) {
component.set('v.columns', [
{label: 'Id', fieldName: 'Id', type: 'text'},
{label: 'LastName', fieldName: 'LastName', type: 'text'}
]);
helper.loadContacts(component, event, helper);
},
loadMoreData: function(component, event, helper) {
console.log('loadMoreData called');
}
})
testDataTableHelper.js
({
loadContacts : function(component, event, helper) {
var action = component.get("c.getContacts");
action.setStorable();
action.setCallback(this,function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var records = response.getReturnValue();
component.set("v.data", records);
component.set("v.enableInfiniteLoading", true);
} else {
console.log('ERROR');
}
});
$A.enqueueAction(action);
}
})
ContactController.class
public class ContactController {
@AuraEnabled
public static List<Contact> getContacts() {
return[Select Id, LastName From Contact Limit 10];
}
}
Lightning component library: lightning:datatable infinite loading