Skip to content

Instantly share code, notes, and snippets.

@svierk
Last active January 27, 2024 17:05
Show Gist options
  • Save svierk/cb293eeda4976e5c91327bf92a78e923 to your computer and use it in GitHub Desktop.
Save svierk/cb293eeda4976e5c91327bf92a78e923 to your computer and use it in GitHub Desktop.
JS Code for Content Document Table LWC | Row Actions
addRowActions() {
const actions = [];
if (this.showDownloadAction) actions.push({ label: 'Download', name: 'download' });
if (this.showViewAction) actions.push({ label: 'View', name: 'view' });
if (this.showDeleteAction) actions.push({ label: 'Delete', name: 'delete' });
if (actions.length) {
this.columns.push({ type: 'action', typeAttributes: { rowActions: actions, menuAlignment: 'right' } });
}
}
handleRowAction(event) {
const actionName = event.detail.action.name;
const row = event.detail.row;
switch (actionName) {
case 'download':
this.download(row);
break;
case 'view':
this.view(row);
break;
case 'delete':
this.delete(row);
break;
default:
break;
}
}
download(row) {
getLatestVersion({ recordId: row.Id })
.then((version) => {
window.open(`/sfc/servlet.shepherd/version/download/${version}`);
})
.catch((error) => {
this.showToast('Error downloading file', error.body.message, 'error');
});
}
view(row) {
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
objectApiName: 'ContentDocument',
actionName: 'view'
}
});
}
delete(row) {
this.isLoading = true;
deleteRecord(row.Id)
.then(() => {
this.showToast('Success', 'File deleted', 'success');
return refreshApex(this.wiredRecords).then(() => {
this.isLoading = false;
});
})
.catch((error) => {
this.showToast('Error deleting file', error.body.message, 'error');
});
}
showToast(title, message, variant) {
this.dispatchEvent(new ShowToastEvent({ title: title, message: message, variant: variant }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment