react-table row onClick #2295
-
I am using react-table latest version. I want to call a function on click of a row, and the function should be passed the data of the row clicked. Any pointers would really help |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 20 replies
-
You can use import * as React from "react";
import { useTable } from "react-table";
type Data = {
actor: string;
movie: string;
}
const borderStyle = {
border: "1px dashed navy"
};
export default function App() {
const origData = [
{
actor: "Johnny Depp",
movies: [
{
name: "Pirates of the Carribean 1"
},
{
name: "Pirates of the Carribean 2"
},
{
name: "Pirates of the Carribean 3"
},
{
name: "Pirates of the Carribean 4"
}
]
}
];
const newData: Array<Data> = [];
origData.forEach(actorObj => {
actorObj.movies.forEach(movie => {
newData.push({
actor: actorObj.actor,
movie: movie.name
});
});
});
const data = React.useMemo(() => newData, []);
const columns = React.useMemo(
() => [
{
Header: "Actor",
accessor: "actor"
},
{
Header: "Movies",
accessor: "movie"
}
],
[]
);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow
} = useTable({ columns, data });
return (
<table {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()} style={borderStyle}>
{column.render("Header")}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()} onClick={() => console.log(row.original)}>
{row.cells.map((cell, j) => {
return (
<td
rowSpan={cell.rowSpan}
{...cell.getCellProps()}
style={borderStyle}
>
{cell.render("Cell")}
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
);
} You can alternatively paste this in Codesandbox & try clicking the rows to see it work :) |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the reply. Also one more thing, how to highlight the selected row on click? I need to style the row as per click. Pls help out.
…Sent from my iPhone
On 20 May 2020, at 6:07 AM, sonalk215 ***@***.***> wrote:
You can call a function(defined in another component) and pass that function as a prop
onClick={()=>props.testRowClick(row)
Hope that answers your question
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
but i cant get it on click..only on hovering its effects comes :(
Thanks and Very best regards
Yasirali
Abudhabi Autonomous Systems Investments
United Arab Emirates
Mobile: +971 (50) 9721325
E-mail: [email protected] <[email protected]>
http://www.adasi.ae/ <http://www.etihad.ae/>
P.O. Box 109667, Abu Dhabi,
…On Thu, May 21, 2020 at 3:49 AM sonalk215 ***@***.***> wrote:
You can set a class to each row
<tr
{...row.getRowProps()}
className={classes['table-body-row']}
onClick={()=>props.testRowClick(row)}
>
and in css
.table-body-row:hover {
background-color: #e6e6ff;
}
where :hover is a css pseudoclass
Hope this helps
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2295 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APUT3IFSGQWXNZ662FAPC7LRSRT7VANCNFSM4M5Q3RFA>
.
|
Beta Was this translation helpful? Give feedback.
-
As i am new, could you please provide a sample code. I didnt get how to add
a class on click. Any help appreciated :)
Thanks and Very best regards
Yasirali
Abudhabi Autonomous Systems Investments
United Arab Emirates
Mobile: +971 (50) 9721325
E-mail: [email protected] <[email protected]>
http://www.adasi.ae/ <http://www.etihad.ae/>
P.O. Box 109667, Abu Dhabi,
…On Thu, May 21, 2020 at 2:02 PM Akshay Kadam (A2K) ***@***.***> wrote:
then add a class on click & style it with css
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2295 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APUT3IF5H4GZ2VISU4YPTMLRST32PANCNFSM4M5Q3RFA>
.
|
Beta Was this translation helpful? Give feedback.
-
reminder pls
Thanks and Very best regards
Yasirali
Abudhabi Autonomous Systems Investments
United Arab Emirates
Mobile: +971 (50) 9721325
E-mail: [email protected] <[email protected]>
http://www.adasi.ae/ <http://www.etihad.ae/>
P.O. Box 109667, Abu Dhabi,
…On Thu, May 21, 2020 at 2:19 PM yasir kv ***@***.***> wrote:
As i am new, could you please provide a sample code. I didnt get how to
add a class on click. Any help appreciated :)
Thanks and Very best regards
Yasirali
Abudhabi Autonomous Systems Investments
United Arab Emirates
Mobile: +971 (50) 9721325
E-mail: ***@***.*** ***@***.***>
http://www.adasi.ae/ <http://www.etihad.ae/>
P.O. Box 109667, Abu Dhabi,
On Thu, May 21, 2020 at 2:02 PM Akshay Kadam (A2K) <
***@***.***> wrote:
> then add a class on click & style it with css
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#2295 (reply in thread)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/APUT3IF5H4GZ2VISU4YPTMLRST32PANCNFSM4M5Q3RFA>
> .
>
|
Beta Was this translation helpful? Give feedback.
-
Does anyone know how to have that same onClick() but only on a button in the last column of the row. I don't want users to click the entire row, just the "Edit" button for the row. If the 'row' is only accessed by 'getRowProps', how can that specificil cell access the row data? |
Beta Was this translation helpful? Give feedback.
-
I want to add a new row(at the end) with the click of the "Add Row" button in react table created. Can someone please me? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
You can use
onClick
ontr
. Here's an example (search foronClick
):