I have a lightning datatable that i would like to preselect all the rows when i load it. I'm currently doing like described in the lighting:datatable definition page but no luck so far
Supposedely this line is preselecting all rows as the selectedRows from datatable is binded to the attribute
component.set('v.selectedRows', selected);
FullCode:
.component
<aura:attribute name="selectedRows" type="List"/>
<lightning:datatable aura:id="partnerTable" data="{!v.partnerdata }"
columns="{!v.partnercolumns}" keyField="Id" selectedRows="
{!v.selectedRows}"/>
.controller
searchPartners : function(component, event, helper) {
helper.searchPartners(component, event, helper);},
.helper
searchPartners : function(component, event, helper) {
var selectedProducts = component.get("v.selectedProducts");
var action = component.get("c.getAccountData");
action.setParams({
"products": selectedProducts
});
var accs=[];
var selected=[];
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var accounts = response.getReturnValue();
accounts.forEach(function(account) {
accs.push({name: account.Name, id: account.Id});
selected.push(account.Id);
});
component.set('v.partnercolumns', [
{label: 'Name', fieldName: 'name', type: 'text'}
]);
component.set('v.partnerdata', accs);
component.set('v.selectedRows', selected);
}
else {
console.log("Failed with state: " + state);
}
});
$A.enqueueAction(action);
},
Id
field one time asid
:accs.push({name: account.Name, id: account.Id});
and another time asId
:selected.push(account.Id);
maybe setting all ids asId
should fix your problem