Apex Aide apexaide

Lightning datatable with dynamic row action

Salesforce Learning Buddy· ·Intermediate ·Developer ·3 min read
Summary

This piece explains how to implement dynamic row actions in a Lightning datatable within Aura components, tailoring the action button label and behavior based on each row's record type. It addresses practical needs for varying user interactions like viewing records, downloading files, or navigating to URLs directly from a datatable. The approach covers backend Apex queries, data transformation, and frontend event handling to create a flexible and user-friendly interface. Salesforce teams can apply these patterns to enhance datatables with customized row actions that adapt to diverse business logic.

Takeaways
  • Use Apex controller to fetch and transform data tailored for datatable display.
  • Customize Lightning datatable columns to include dynamic button actions per row.
  • Implement client-side JS handlers to perform distinct logic based on row action label.
  • Leverage record type to determine and set different action labels per datatable row.
  • Use Lightning navigation events to open external URLs from datatable row actions.

Lightning data tables are very very common for aura components. We will see how see can we have different row action name and different behaviour for data table Here is the query List<Resource__c> resourceList = [Select Id,Name,Week__c,Type__c,RecordType.Name,Resource_URL__c,Description__c,Curriculum__c from Resource__c where Curriculum__c IN :curriculumSet ORDER BY Week__c ASC]; List<ResourceRecord> rrList = new List<ResourceRecord>(); for(Resource__c res : resourceList){ ResourceRecord rec = new ResourceRecord(); rec.resourceName=res.Name; rec.week=res.Week__c; rec.recordId=res.Id; rec.recordtypename=res.recordtype.Name; rec.type=res.Type__c; rec.url=res.Resource_URL__c; rrList.add(rec); } return rrList; I will only include the relevant section in the aura component for better clarity Component <lightning:datatable data="{!resourceData}" columns="{!v.resourceTableColumns}" keyField="Id" showRowNumberColumn="false" onrowaction="{!c.

ApexSalesforce