The lightning-datatable component fires several custom events, including the rowselection event when a row is selected.
For more information on the custom events available for lightning-datatable, see the lightning-datatable reference documentation.
If these custom events don’t meet your requirements, you can dispatch your own custom event and handle it from your parent component.
This documentation uses the term lightning-datatable component and datatable interchangeably.
Note
Example: Navigate to a Record with a Custom Data Type
Here’s an example that navigates to a record when a custom data type is clicked. The datatable loads a Contact Picture column using a custom data type.
The customDataTypes.js file imports and defines the customPicture data type within the same folder. To make the record ID available when the photo is clicked, the custom data type includes the recordId type attribute.
customDataTypes.js
1import LightningDatatable from "lightning/datatable";2import customPicture from "./customPicture.html";3export default class CustomDataTypes extends LightningDatatable{4 static customTypes = {5 customPictureType:{6 template: customPicture,7 standardCellLayout: true,8 typeAttributes:["pictureUrl", "recordId"],9},10 // Other Custom Types11};12}
The customPicture template passes in the pictureUrl and recordId type attributes to the c-custom-pic child component, which is a new component you can create on your own that’s not part of the lwc-recipes repo.
The ContactController.cls Apex class returns a list of contacts.
ContactController.cls
1public with sharing class ContactController{2 @AuraEnabled(cacheable=true)3 public static List<Contact>getContactList(){4 return[5 SELECT6 Id,7 Name,8 FirstName,9 LastName,10 Title,11 Phone,12 Email,13 Picture__c14 FROM Contact15 WHERE Picture__c != NULL16 WITH USER_MODE17 LIMIT 1018];19}20}
Dispatch a Custom Event via a Slot
If you dispatch a custom event on a custom data type via the customdatatypes slot, you can handle the custom event on the lightning-datatable component. For example, add the onphotoclick handler like this.
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.