我有一个闪电:数据表,我需要在用户滚动时加载数据。我发现当用户单击表格行而不是用户在表格上滚动时会触发 onloadmore 事件。
更有什者,一旦触发了onloadmore,它就会一直调用,直到加载数据库中的所有记录。
有人可以帮我理解为什么要这样做和解决方法吗?任何帮助将不胜感激。
要检查如何调用 loadMoreData 函数,请打开检查器并在单击一行后检查控制台。
这是一个例子:
测试数据表.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 组件库:lightning:datatable 无限加载