Bind disable attribute dynamically for each row for disabling button row-wise in LWC

javascriptlightning-web-components

[![enter image description here][1]][1]I have a situation in LWC. In a table there are multiple records coming dynamically with selection of available dropdowns. Each row has Approve and Reject button functionality. I have managed Approve and Reject by a custom field. Here after Approval/Rejection I want to disable buttons for selected approved row only.
But because of same disable attribute it disables approve/reject buttons for all unselected rows – that I don't want. I want button disable for only selected row..
Disable attribute in Lightning button for approve/reject is same. disable=(isDisabledButton). How could I achieve this scenario?

HTML CODE

<template for:each={materialMap} for:item="item">
    <tr class="slds-hint-parent" key={item.id}>
        <td class="slds-cell-wrap" scope="col" >
            <div class="slds-truncate slds-cell-wrap">
                {item.materialName__c}
            </div>
        </td>
        <td class="slds-cell-wrap" scope="row">
            <div class="slds-truncate slds-cell-wrap">
                {item.avgCount}
            </div>
        </td>
        <td class="slds-cell-wrap" scope="row">
            <div class="slds-truncate slds-cell-wrap">
                {item.totalValue}
            </div>
        </td>
        <td class="slds-cell-wrap" scope="row">
            <div class="slds-truncate slds-cell-wrap">
                {item.discountedValue}
            </div>
        </td>
        <td if:true={showApproveRejectbutton} class="slds-cell-wrap" scope="row">
            <div class="slds-truncate slds-cell-wrap">
                <lightning-button variant="success" name={item.materialName__c} label="Approve" title="Successful action" onclick={handleApproval} class="slds-m-left_x-small" disabled={isDisabledButton}>
                </lightning-button>
            </div>
        </td>
        <td if:true={showApproveRejectbutton} class="slds-cell-wrap" scope="row">
            <div class="slds-truncate slds-cell-wrap">
                <lightning-button variant="destructive" name={item.materialName__c} label="Reject" title="Destructive action" onclick={handleApproval} class="slds-m-left_x-small" disabled={isDisabledButton}>
                </lightning-button>
            </div>
        </td>
    </tr>
</template>

JS

handleApproval(event){              
    const actionButton = event.target.name;
    let selectedButton= event.currentTarget.name; 
    console.log('selectedButton '+selectedButton);
    let objIndx = this.materialMap.findIndex((item => item.materialName__c== selectedButton));
    console.log('objIndx== '+objIndx);
    let buttonvalue= this.materialMap[objIndx].isDisabledButton=true;
    console.log('Name of buttonvalue  = '+JSON.stringify(buttonvalue));
}

For this

console.log('Name of buttonvalue = '+JSON.stringify(buttonvalue));

I am getting true value but it is not disabling the buttonenter image description here

Best Answer

welcome to the community.

In your question you have also pointed out the flaw in this approach "Disable attribute in lightning button for approve/reject is same".

To achieve this, I would suggest having "disabled" attribute as part of the Data on which you are iterating, so that you can use "item.disabled" instead of the current boolean (isDisabledButton).

when you select something, in the change handler just update the disabled to true for that item in your js file.

Related Topic