Last active
January 27, 2024 17:05
-
-
Save svierk/cb293eeda4976e5c91327bf92a78e923 to your computer and use it in GitHub Desktop.
JS Code for Content Document Table LWC | Row Actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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