I am using lightning-datatable with an onrowaction. My JS code to handle row action is as follows:
How do I refresh the datatable after every edit.
handleRowAction( event ) {
const assignmentId = event.detail.row.Id;
const actionName = event.detail.action.name;
const row = event.detail.row;
switch ( actionName ) {
case 'view':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
actionName: 'view'
}
});
break;
case 'edit':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
objectApiName: 'Assignment__c',
actionName: 'edit'
}
});
break;
case 'delete':
deleteRecord(assignmentId)
.then(() => {
this.dispatchEvent(
new ShowToastEvent({
title : 'Success',
message : JSON.stringify(event.detail.row.Name) + ' deleted',
variant : 'success'
})
);
return refreshApex(this.refreshTable);
});
break;
default:
}
}
check this code. It should update the record only once and refresh the datatable after the update is complete.
handleRowAction(event) {
const assignmentId = event.detail.row.Id;
const actionName = event.detail.action.name;
const row = event.detail.row;
switch (actionName) {
case 'view':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
actionName: 'view'
}
});
break;
case 'edit':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
objectApiName: 'Assignment__c',
actionName: 'edit'
}
});
break;
case 'delete':
deleteRecord(assignmentId).then(() => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: JSON.stringify(event.detail.row.Name) + ' deleted',
variant: 'success'
})
);
return refreshApex(this.refreshTable);
});
break;
case 'save':
const fields = {};
fields['Id'] = row.Id;
fields['Student_Name__c'] = row.Student_Name__c;
const recordInput = { fields };
updateRecord(recordInput)
.then(() => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: JSON.stringify(event.detail.row.Name) + ' updated',
variant: 'success'
})
);
return refreshApex(this.refreshTable);
})
.catch((error) => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error updating record',
message: error.body.message,
variant: 'error'
})
);
});
break;
default:
break;
}
}