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?

2 Answers 2


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.

let rows = this.template.querySelector('lightning-datatable').getSelectedRows();

Or you can use the onrowselection event handler to get the selected rows.




getSelectedName(event) {
    const selectedRows = event.detail.selectedRows;
    // Display that fieldName of the selected rows
    for (let i = 0; i < selectedRows.length; i++){
        console.log("You selected: " + selectedRows[i].opportunityName);

Documentation for Lwc datatable.

  • I have used the above code. But, I don't have any unique field to compare the records and find the selected row number. Any alternate way?
    – Ajinkyasd
    Jul 13, 2020 at 8:52
  • I am displaying the records to be inserted in the table, and want to implement the delete functionality, so that user can delete the records before saving to the database.
    – Ajinkyasd
    Jul 13, 2020 at 8:54
  • If you can create a custom unique field on the data like index @Ajinkyasd Jul 13, 2020 at 9:20
  • @Ajinkyasd you can check this playground I had created long back, it has delete functionality as well as the unique id generation code, search for uid. Jul 13, 2020 at 11:39

This may be an old question but it is still not possible to get directly the index of the selected row, which I think should be an easy operation.

My workaround has been to add the index in the data row myself, as an extra property that is not visible on the table.

Sample Code

Retrieve index in code

Example in console

You must log in to answer this question.

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