[SalesForce] LWC Datatable – Get the Row Number when editing a column

As the title says, has anyone tried to get the row number when editing a column as a draft value (Quantity in the image reference below). So for example, if I add "2" to the Quantity column in Row 1, I can fetch the row number "1".

Anyone has done anything similar? Any advice, code snippets, pointers are helpful.

enter image description here

Best Answer

You don't get the row number, but you do get the key-field value. This is sufficient to find the matching row.

var rowIndex = this.data.findIndex(row => row.id === event.detail.draftValues[0].id);

Remember arrays are zero-based, so if for some reason you need the row number as it appears in the table, add 1.

In case you just want the actual row that's in memory, use find instead:

var row = this.data.find(row => row.id === event.detail.draftValues[0].id);