Hi Everyone, In this post we will see an example that let you view, edit and delete record through lightning datatable. These task will be done through datatable action attribute which is really helpful to do all thing from single place.
View, edit and delete record through datatable
In this example, I am using account record to perform all these action, any standard and custom object will be used for this example.
DatatableHandler.apxc
public class DatatableHandler {DatatableActions.cmp
@AuraEnabled
public static List<Account> getAccountData(){
return [SELECT Id,Name,AccountNumber,Industry,Phone FROM Account limit 7];
}
@AuraEnabled
public static void delAccount(Account accountRec){
delete accountRec;
}
}
<aura:component controller="DatatableHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId" access="global" >DatatableActionsController.js
<aura:attribute name="results" type="List[]"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="isLoading" type="Boolean" default="false" />
<aura:if isTrue="{! v.isLoading }">
<lightning:spinner alternativeText="Loading" />
</aura:if>
<lightning:layout multipleRows="true" horizontalAlign="center">
<lightning:layoutItem padding="around-small" size="12">
<lightning:datatable keyField="id" data="{!v.results}"
columns="{!v.mycolumns}"
resizeColumnDisabled="true"
hideCheckboxColumn="true"
onrowaction="{!c.handleRowAction}"
/>
</lightning:layoutItem>
</lightning:layout>
</aura:component>
({DatatableActionsHelper.js
doInit : function(component, event, helper) {
var actions = [
{label: 'View', name: 'view'},
{label: 'Edit', name: 'edit'},
{label: 'Delete', name: 'delete'}
];
component.set('v.mycolumns', [
{label: 'Name', fieldName: 'Name', type: 'text'},
{label: 'AccountNumber', fieldName: 'AccountNumber', type: 'text'},
{label: 'Industry', fieldName: 'Industry', type: 'text'},
{label: 'Phone', fieldName: 'Phone', type: 'phone'},
{type: 'action', typeAttributes: { rowActions: actions } }
]);
helper.pullData(component);
},
handleRowAction: function (component, event, helper) {
var action = event.getParam('action');
switch (action.name) {
case 'view':
helper.viewRecord(component, event);
break;
case 'edit':
helper.editRecord(component, event);
break;
case 'delete':
helper.deleteRecord(component, event);
break;
}
},
})
({
pullData:function(component){
component.set("v.isLoading", true);
var action=component.get("c.getAccountData");
action.setCallback(this,function(e){
component.set("v.isLoading", false);
if(e.getState()=='SUCCESS'){
var results=e.getReturnValue();
if(results.length>0){
component.set('v.results', results);
}
else{
component.set('v.results', []);
}
}
else{
this.showToast("ERROR","error",JSON.stringify(e.getError()));
}
});
$A.enqueueAction(action);
},
viewRecord : function(component, event) {
var row = event.getParam('row');
var recordId = row.Id;
var viewRec=$A.get("event.force:navigateToSObject");
viewRec.setParams({
"recordId": recordId,
"slideDevName": "detail"
});
viewRec.fire();
},
editRecord : function(component, event) {
var row = event.getParam('row');
var recordId = row.Id;
$A.get("e.force:editRecord").setParams({"recordId": recordId}).fire();
},
deleteRecord : function(component, event) {
component.set("v.isLoading", true);
var accountRec = event.getParam('row');
var action = component.get("c.delAccount");
action.setParams({
"accountRec": accountRec
});
action.setCallback(this, function(response) {
component.set("v.isLoading", false);
if (response.getState() === "SUCCESS" ) {
var rows = component.get('v.results');
var rowIndex = rows.indexOf(accountRec);
rows.splice(rowIndex, 1);
component.set('v.results', rows);
this.showToast("Success!","success","The record has been delete successfully.");
}
else{
this.showToast("ERROR","error",JSON.stringify(response.getError()));
}
});
$A.enqueueAction(action);
},
showToast:function(title,type,message){
var toastEvent = $A.get("e.force:showToast");
if(toastEvent){
toastEvent.setParams({"title": title,"type": type,"message": message}).fire();
}
else{
alert(message);
}
},
})
Output:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
16 Comments
Im not getting view and edit action on result please help for same
ReplyDeletecode send to above mail please help
ReplyDeletehi if we want to create a button to create a record by using the record then how we can do it
ReplyDeleteCan't understand your question, Please post it again.
DeleteThanks
instead of dropdown ,how can we put seperate icon for edit,delete
ReplyDeleteand view?
no, but you can add button for each action...
Deletesir im not getting view and edit . its only perform delete.
ReplyDeleteadd this component in lightning page and run in lightning experience, this definitely work.
DeleteI am unable to display the formula fields values in Data Table.
ReplyDeleteWhy are there two var action in deleteRecord?
ReplyDeletefirst one was put by mistake, but that not impact your code. You can remove first action var.
Deletehow these things can be added : lightning Datatable, search bar and List View
ReplyDeleteDatatable should have an inline edit option for individual fields, and should also support edit record and delete record actions.and Name and Owner Column should be a link.
You can find solution for your all questions in my blog. Please read and implement those posts that belongs to lightning menu.
Deletei am not getting edit option working. 'c:DatatableActions$controller$handleRowAction [Cannot read properties of undefined (reading 'setParams')]' this is the error
ReplyDeleteadd this component in lightning page and run in lightning experience, this definitely work.
DeleteOn View Record :
ReplyDeleteThis page has an error. You might just need to refresh it.
Action failed: c:DynamicListComponent$controller$handleRowAction [Cannot read properties of undefined (reading 'setParams')]
Failing descriptor: {c:DynamicListComponent$controller$handleRowAction}
Post a Comment