[SalesForce] Dynamic row actions in Data table using LWC

Can someone help me how to code a dynamic row actions in data table using LWC?

I want to publish the book if the field value is unpublished and I also have the choice to unpublished the book if the field value is published. Please help I really need this for my assignment.
Thank you so much

Best Answer

Set rowActions to a method:

{ type: 'action', typeAttributes: { rowActions: this.getRowActions }

Then return the actions you want in that method:

getRowActions(row, doneCallback) {
  if(row.Status__c==='Unpublished') {
    doneCallback([{ label: 'Publish', name: 'publish' }]);
  }
  if(row.Status__c==='Published') {
    doneCallback([{ label: 'Unpublish', name: 'unpublish' }]);
  }
}

You can read all about this in the documentation.