1

How can I get data from a row selected in a lightning data table? I was looking at this post but it doesn't seem like the playground linked in the answer is working so I cannot see the helper method. The first answer for that uses a custom table and I would like to stick to using the lightning datatable. Does anyone have any examples? I would like to get the recordId's of the records selected, to be specific.

1
  • were you able to get your row selection working?
    – glls
    Aug 14, 2021 at 15:49

2 Answers 2

4

The playground / lwc documentation has a clear code sample of how to get row selection(s):

    <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}
                onrowaction={handleRowSelection}>
        </lightning-datatable>

    handleRowSelection(event){
            const selectedRows = event.detail.selectedRows
    }
1

The answer that glls gave is close but not working. What does work is:

    <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}
                onrowselection={handleRowSelection}>
     </lightning-datatable>

    handleRowSelection(event){
            const selectedRows = event.detail.selectedRows
    }

Notice that instead of onrowaction, the correct element is onrowselection.

You must log in to answer this question.

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