Search code examples
reactjsmui-datatable

mui-datatable: Get row data from onRowsDelete


I have an mui-datatable with user data. When a row is selected and then Delete is clicked, I would like a function to also be called that deletes the object displayed in the row from the database as well. How can I access the object from the row? I only get the following array back so far.

data: Array(1)
  0:
    dataIndex: 0
    index: 0
  length: 1

Code

const options: MUIDataTableOptions = {
    onRowsDelete: (e) => deleteSelectedUser(e)
}


async function deleteSelectedUser(e: any) {
    await deleteUser().then((result) => {
        console.log(result)
    })
}

return (
    <div>
        {
            users.length > 0 &&
                <MUIDataTable
                title={"Employee List"}
                data={users}
                columns={columns}
                options={options}
                />
        }
    </div>
)

}


Solution

  • The index of the deleted row is in e, try console log it.

    async function deleteSelectedUser(e: any) {
        console.log(e); // <===================== ADD THIS LINE TO DEBUG
        await deleteUser().then((result) => {
            console.log(result)
        })
    }
    

    You need to use the dataIndex to find the corresponding object for the delete event