I am not able to retrieve the correct line of the datatable on my LWC. Ive followed several searches but apparently Im still missing something.
Here is the HTML:
<lightning-datatable
key-field="id"
data={usersData}
columns={usersColumns}
onrowaction={handleRowAction}
show-row-number-column
hide-checkbox-column="true">
</lightning-datatable>
here is the JS declaration and code:
const actions = [
{ label: 'Delete', name: 'delete' },
];
const columns = [
{ label: 'Name', fieldName: 'Name' },
{ label: 'Access Level', fieldName: 'AccessLevel' },
{
type: 'action',
typeAttributes: { rowActions: actions },
}
];
....
handleRowAction(event) {
let actionName = event.detail.action.name;
let row = event.detail.row;
switch (actionName) {
case 'delete':
this.deleteRow(row);
break;
default:
}
}
deleteRow(row) {
const { id } = row;
const index = this.findRowIndexById(id);
console.log(index);
}
findRowIndexById(id) {
let ret = -1;
this.usersData.some((row, index) => {
if (row.id === id) {
ret = index;
return true;
}
return false;
});
return ret;
}
It is always printing 0, even if I select the second or third lines.