Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get React Table Row Data Onclick

Hi I am trying to set up my react app such that when you click on a button in a row item in my react-table the data in that row is passed onto another component. At the moment I am simply trying to console.log the correct data but am unsure of how to pass react-table row data based on click. How can I do this? Thanks

My Dummy data stored in state along with the button (Show Detailed View) which I want to trigger the data passing onclick:

    columns: [
      {
        Header: "First Name",
        accessor: "fname"
      },
      {
        Header: "Last Name",
        accessor: "lname"
      },
      {
        Header: "Employee Group",
        accessor: "egroup"
      },
      {
        Header: "Date of Birth",
        accessor: "dob"
      },
      {
        Header: "",
        id: "id",
        Cell: ({ row }) => (
          <button onClick={e => this.handleShow()}>
            Detailed View
          </button>
        )
      },
    ],
    posts: [
      {
        fname: "gerald",
        lname: "nakhle",
        egroup: "faisbuk",
        dob: "8/10/1995"
      }
    ]

My Call to render the table:

<ReactTable columns={this.state.columns} data={this.state.posts}></ReactTable>

My onclick handler function but im not sure how I can access the table row data I'm after

handleShow(e) {
    console.log(e);
  }
like image 652
Tamjid Avatar asked May 11 '19 01:05

Tamjid


3 Answers

You will need to add an onClick handler for the row

const onRowClick = (state, rowInfo, column, instance) => {
    return {
        onClick: e => {
            console.log('A Td Element was clicked!')
            console.log('it produced this event:', e)
            console.log('It was in this column:', column)
            console.log('It was in this row:', rowInfo)
            console.log('It was in this table instance:', instance)
        }
    }
}

<ReactTable columns={this.state.columns} data={this.state.posts} getTrProps={onRowClick}></ReactTable>

check this post for more info react-table component onClick event for a column

like image 129
Khalid Avatar answered Sep 29 '22 15:09

Khalid


In your Table definition :

export function TableCustom({handleShow}) {
    const columns = React.useMemo(() => [{
        Header: 'Action',
        accessor: 'action',
        Cell: props => <button className="btn1" onClick={() => handleShow(props)}>Details</button>,
    },]

    return <ReactTable>;
});

And in your parent component : view the data of the clicked row :

const handleShow = (cell) => {
    console.log(cell?.row?.original);
}
like image 21
pushStack Avatar answered Sep 29 '22 16:09

pushStack


For React-table v7:

Pass a callback prop onRowClicked to the table component,

In your table component call the callback:

...row.getRowProps({
         onClick: e => props.onRowClicked && props.onRowClicked(row, e),
})

In react-table v7, all the spread operator on HTML element that starts with get...Props are props getter, for example:

row.getRowProps(), cell.getCellProps(), column.getHeaderProps(), getTableBodyProps(), getTableProps() etc.. You can pass any more attributes to extend it. for example:

    ...cell.getCellProps({ 
        style: {color: 'red'},  
        onClick: ()=> {}   
    }) 
like image 35
Harish Kulkarni Avatar answered Sep 29 '22 17:09

Harish Kulkarni