[SalesForce] Pre Select Rows in Lightning Data Table

I want to Preselect all data in my Lightning Data Table. for that, I have added selected-rows also. But not working. My code

<lightning-datatable 
    data={data}
    selected-rows={data}
    columns={columns}
    key-field="Id"
    column-widths-mode="auto"
    > 
</lightning-datatable>

Note: I want to pre-select all the displayed data hence used data in both data and selected-rows.

Thanks
Nirav

Best Answer

selected-rows should be bound to a list of key-field values (Ids, in this case), not to a list of records

Enables programmatic row selection with a list of key-field values.

per the LWC Developer Guide.

You'd need in your component to create a new field to store the selected Ids, and prepopulate it at load time with the Ids of all of your records. Then, bind selected-rows to that field.

Related Topic