It's possible to preselect items, however I noticed a bug on API v42 (current version) and I confirmed that with SFDC support team. When using the attribute to bind the selected rows, it simply doesn't work, however if you find the lightning:datatable and then change the selectedRows, it works properly.
component = component.find("partnerTable");
component.set("v.selectedRows", selectedRowsIds);
Please find an example below, I'm creating a mockdata and selecting the first item by the Id.
Component
<aura:attribute name="partnerdata" type="Object" access="private" />
<aura:attribute name="partnercolumns" type="List" access="private" />
<aura:attribute name="partnerSelectedRows" type="List" access="private" default="" />
<!--
<lightning:datatable aura:id="partnerTable" data="{!v.partnerdata}"
columns="{!v.partnercolumns}" keyField="Id" selectedRows="{!v.partnerSelectedRows}"/>
-->
<lightning:datatable aura:id="partnerTable" data="{!v.partnerdata}"
columns="{!v.partnercolumns}" keyField="Id" />
Controller
({
doInit : function(component, event, helper) {
helper.getMockData(component, event, helper);
}
})
Helper
({
getMockData: function(component, event) {
console.log("Load mock data!");
component.set("v.partnercolumns", [{
label: "Name",
fieldName: "Name",
type: "text"
},
{
label: "Status",
fieldName: "status__c",
type: "text"
},
{
label: "Created Date",
fieldName: "createdate__c",
type: "date"
}
]);
component.set("v.partnerdata", [{
Id: "a0319000001GtsjAAC",
Name: "John Doe",
status__c: "Active",
createdate__c: "2005-01-01"
},
{
Id: "a0319000001GtsjAAD",
Name: "Mary Doe",
status__c: "Active",
createdate__c: "2005-02-10"
}
]);
var selectedRowsIds = ["a0319000001GtsjAAC"];
// I was expecting the line below to work
//component.set("v.partnerSelectedRows", selectedRowsIds);
// Workaround to selectRows
component = component.find("partnerTable");
component.set("v.selectedRows", selectedRowsIds);
}
})
Old: Out of the box <lighting:datatable>
component doesn't support booleans as checkboxes in the standard data types, the supported data types currently are:
- action
- button
- currency
- date
- email
- location
- number
- percent
- phone
- text
- url
These can be found under the "Formatting with Data Types" section on the Lightning datatables reference documentation
A quick workaround to this would be to output a text formula of the Paid__c
field to display Yes / No or Paid / Unpaid and then to output this in the column.
Other than this you may need to look into more flexible table options which would allow for the use / display of checkbox fields.
Edit: From API Version 42 onwards the Boolean type is now supported.
Best Answer
Just for the information.
I had the same issue and as a workaround I implemented following.
Note: I was using lightning web components
JS Code
HTML Code
Then if your condition is met, you cannot select that particular row.