2

I have a requirement to show popovers, when clicking on table cell (used Material UI table cell). Tried adding onclick function to cell, but not able to pass the element value to the function. How can I achieve this? Below is the code snippet which used to render table.

<TableBody>
 {dataObject.map((data) => (
    <TableRow key={data.id}>
    <TableCell>
        <Button>
            {data.info}
        </Button>
    </TableCell>
    <TableCell>
        {data.request}
    </TableCell>
    </TableRow>
))}
</TableBody>        

Thanks in advance.

0

1 Answer 1

8

The <TableCell> component is eventually a td/th in your html, so you don't really have a value attached to it (it's not like a pure react component that you can just use value, or an input element that has value that you can access).

What you can do is access the element's innerHTML / textContent to access the content of the cell using the event itself:

const handleCellClick = (e) => {
    console.log(e.target.textContent);
}

<TableCell onClick={handleCellClick}>{data.request}</TableCell>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks.. this is what I was looking.
@ Dekel, is it possible to get the values of other columns in same row using the event.target?
Everything is possible, but I really wouldn't suggest it to go this way.
What would be the appropriate method in such scenario?
I would probably use the material-table (which is a component that gives you such functionality), and make sure that onClick I can access the data of the row using the row object (and not only the cell using the textContent).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.