Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access row's data via onClick in <td> element?

In the below shown Users.jsx file I am populating a table with the data stored in the userList array. I have implemented an onClick listener in a table data cell element, I want the Trigger function to be called with the clicked row’s info object. Or I want to be able to access the clicked row’s data in the Trigger function.

import React from 'react';
import '../../node_modules/bootstrap/dist/css/bootstrap.min.css'
import Navbar from "../components/NavBar";
import { Table } from 'react-bootstrap';

function Users() {

  let userList = [
    {'name': 'Jack',
    'age': 21,
    'role': 'Customer',
    },
    {'name': 'Tom',
    'age': 22,
    'role': 'Product Manager',
   }]

   const Trigger = () => {
     // Here
   }
  
  const UserData = userList.map(
    (info)=>{
      return(
          <tr>
          <td>{info.name}</td>
          <td>{info.age}</td>
          <td>{info.role}</td>
          <td onClick={Trigger}> Visit Profile  </td>
        </tr>
      )
    }
  )

  return(
    <div>
      <Navbar/>
      <div>
        <div className="row">
          <div className="col-md-8 offset-md-2">
            <Table striped bordered hover responsive="md">
                <thead>
                    <tr>
                    <th>NAME</th>
                    <th>AGE</th>
                    <th>ROLE</th>
                    <th>PROFILE</th>
                    </tr>
                </thead>
                <tbody>
                    {UserData}                    
                </tbody>
            </Table>
            </div>
            </div>
      </div>
    </div>
  )

}

export default Users;

Any help with a solution or recommendation to documentation for (event handlers needed in this context) will be much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

function Users() {

  let userList = [
    {'name': 'Jack',
    'age': 21,
    'role': 'Customer',
    },
    {'name': 'Tom',
    'age': 22,
    'role': 'Product Manager',
   }]

   const Trigger = (info) => {
    // info it's user obj
   }
  
  const UserData = userList.map(
    (info)=>{
      return(
          <tr>
          <td>{info.name}</td>
          <td>{info.age}</td>
          <td>{info.role}</td>
          <td onClick={()=>{Trigger(info)}}> Visit Profile  </td>
        </tr>
      )
    }
  )

  return(
    <div>
      <Navbar/>
      <div>
        <div className="row">
          <div className="col-md-8 offset-md-2">
            <Table striped bordered hover responsive="md">
                <thead>
                    <tr>
                    <th>NAME</th>
                    <th>AGE</th>
                    <th>ROLE</th>
                    <th>PROFILE</th>
                    </tr>
                </thead>
                <tbody>
                    {UserData}                    
                </tbody>
            </Table>
            </div>
            </div>
      </div>
    </div>
  )

}

export default Users;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading