[SalesForce] How to conditionally add the “show-row-number-column” attribute to the lightning-datatable

I am new to LWC and I'm trying to learn by converting an older Aura component to a LWC. What I'm trying to do right now is conditionally add the "show-row-number-column" attribute to the lightning-datatable via JavaScript.

In Aura I could set showRowNumberColumn = "{! c.enableRowNumber}" (which would pull in true or false conditionally), but in the LWC it looks like I have to add or remove the whole attribute from the datatable not just set it to true or false. I've searched, but don't see any way to conditionally add or remove an attribute from the datatable. Would someone be able to point me in the right direction here?

Best Answer

Although it is true that you have to just mention the attribute to make it true in LWC, this is applicable only to pass static value true and if you want to make it dynamic you can bind it to tracked property in LWC also (like Aura).

show-row-number-column={enableRowNumber}

Here show-row-number-column will become true or false depending on tracked property enableRowNumber.

PROOF: You can this playground link - created an example for your understanding