I am displaying the records in the table which are to be inserted, and want to implement the delete functionality so that the user can delete the records before saving it to the database. There is no unique field or record id.
On click of a button, I want to get the selected row number from lightning-datatable. For example, if I select 3rd and 5th row in the lightning-datatable, on click of a button, I should get 3 and 5 row number.
I tried this.template.querySelector('lightning-datatable').getSelectedRows();
, but I am getting complete row data.
How can I get selected row number?
Best Answer
You can not get the row numbers but you can get the selected rows. You can use
getSelectedRows()
method. Based on the selected rows you can find the selected records from the list of all records.Or you can use the
onrowselection
event handler to get the selected rows.HTML
JS
Documentation for Lwc datatable.