0

I am using a wrapper class to get the document details of an opportunity and display them using a custom LWC component using a lighting data table. I want to hide/disable the row action button (Share with Client) on the lighting data table when the field (hideDocTransferButton) value is true.

Documents Type Data Table & Wrapper Record Data

The field value is coming as true based on the certain condition we set in the Apex controller method. As I am setting the disabled property value of action typeAttributes based on the field value.

JS

    const actions = [
        { label: 'Share with Client', name: 'share' },
    ];
    
    const columns = [
        { 
            label: 'Title', 
            fieldName: 'docURL',
            type: 'url',
            typeAttributes: {label: { fieldName: 'Title' },
                target: '_blank'}, 
            initialWidth: 300,
            sortable: "true"
        },
        { label: 'Document Type', fieldName: 'DocType', sortable: "true"},
        { label: 'Date uploaded', fieldName: 'createdDate', type: 'date', sortable: "true" },
        { 
            label: 'Shared with client', 
            fieldName: '', 
            cellAttributes: { iconName: { fieldName: 'TransferStatus' },
            class: 'slds-align_absolute-center' },
            sortable: "true" 
        },
        { label: 'Date shared with client', fieldName: 'TransferedDate', type: 'date', sortable: "true" },
        { label: 'Uploaded by', fieldName: 'createdBy', sortable: "true" },
        {
            type: 'action',
            typeAttributes: { rowActions: actions, disabled: { fieldName: 'hideDocTransferButton' } },
        },
    ];

    export default class DatatableWithRowActions extends LightningElement {
        @track data = [];
        @api recordId;
        columns = columns;
        record = {};
        @track sortBy;
        @track sortDirection;
       
        selectedRows=[];
        selectedData=[];
        cardTitle = 'Files'
        _wiredResult;
        loaded = false;
        
        
        @wire(getDocumentList, {recordId: '$recordId'} )
        wiredCallback(result) {
            this._wiredResult = result;
            if (result.data) {
                this.data = result.data;
                console.log('******this.data*****', JSON.parse(JSON.stringify(this.data)));
                this.loaded = true;
            } 
        }
        
        handleRowSelection(event) {
            let selectedRows = event.target.selectedRows;        
            this.selectedRows = selectedRows;
        }
    
        handleRowAction(event) {
            const actionName = event.detail.action.name;
            const row = event.detail.row;
            switch (actionName) {
                case 'share':
                    this.handleTransAction(row);
                    break;
                default:
            }
        }
    }

HTML

<div class="slds-card__body slds-p-bottom_large" style="margin-top:-8px" >
                    <lightning-datatable
                        key-field="Id"
                        data={data}
                        columns={columns}
                        onrowaction={handleRowAction}
                        hide-checkbox-column="true"
                        sorted-direction={sortDirection}
                        sorted-by={sortBy}
                        onsort={doSorting}
                        >
                    </lightning-datatable>
                </div>

I have tried similar approach provided here Solution1, Solution2, Solution3 and followed the same logic to hide/disable the action. However, the button is not disabled when the field value is true and I am unable to find what is wrong with code. Can someone please help me with this issue? Thank you!

4
  • Have you checked this? - Creating Dynamic Row-Level Actions Commented Jul 15 at 12:34
  • @TusharJadav - Thanks for replying! I did check the sample code, which dynamically pushes the action buttons. In my case, the requirement is to hide/disable the action button for all the rows based on the property value, which comes from the Apex wrapper class. So, I am looking for a direct approach, if any, to disable the button and instead dynamically push the action button.
    – Giri K
    Commented Jul 15 at 12:41
  • 1
    in Creating Static Row-Level Actions section of documentation it mentions that action have 'disabled' attribute option, so you should be able pass objects from Apex with disabled attribute specified based on your criterias
    – ytiq
    Commented Jul 15 at 13:51
  • @ytiq - Thanks for your reply! I am setting the hideDocTransferButton property to true or false based on a few field values in the Apex controller and passing on to the row action as below: { type:'action', typeAttributes: { rowActions:actions, disabled: { fieldName:'buttonDisabled' } }, } However, it doesn't seems working.
    – Giri K
    Commented Jul 15 at 14:37

1 Answer 1

0

I think it's better if I reply here. I think what you need to do is to something like this.

    columns = [
        // ...
        { type: 'action', typeAttributes: { rowActions: this.getRowActions } },
        // ...    
    ];

    getRowActions(row, doneCallback) {
        const actions = [];
        if (row.buttonDisabled) {
            actions.push({
                label: 'Share with Client', 
                name: 'share',
                disabled: true
            });
        } else {
            actions.push({
                label: 'Share with Client', 
                name: 'share',
                disabled: true
            });
        }

        doneCallback(actions);
    }

Because I think you need to check buttonDisabled value in the actual function. So hopefully this snippet will work for you.

3
  • 1
    The code works fine; thank you very much. Perhaps @TusharJadav also suggested the same solution, but I missed the disabled: true. Thank you @TusharJadav. I added the below code as I cannot access the getter method outside the class. constructor() { super(); this.columns.push({ type: 'action', typeAttributes: { rowActions: this.getRowActions } },); }
    – Giri K
    Commented Jul 15 at 17:01
  • please mark my answer as correct, so that we both get reputation and question gets closed
    – ytiq
    Commented Jul 16 at 7:53
  • Thank you. I marked your comments as the answer.
    – Giri K
    Commented Jul 17 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .