简体   繁体   中英

How to display data to the inputs when the specific table row is clicked

I am new in React and I would like to know how can I do that When I click the button in a specific row in the table I will get that specific data for that row and get it displayed in the input fields in the same form where you can add that data to the table.

I hope that my question makes sense. Thank you for every answer:)

The way you would do this completely depends on your table component and form handling. Are you using HTML forms and using state to manage your own form? Are you using formik ?. Assuming you're dynamically generating the table rows with plain table components, you could do something like the following to give the button in each row access to the data in the row when clicked.

const data = [
  { name: 'Joe Shmoe', age: 25 },
  { name: 'Sarah Silverman', age: 40 },
  { name: 'Ronald Lee', age: 10 },
];

const Table = () => {
  return (
    <table>
      <tbody>
        {data.map(d => (
          <tr>
            <td>{d.name}</td>
            <td>{d.age}</td>
            <button onClick={() => alert(d)}>Click me</button>
          </tr>
        ))}
      </tbody>
    </table>
  )
};

If you could share your code, I get more understanding of your question. Answer of your question according to my observation:

Make one state that will store your selected row. Set this state with row's data when user click on the button.

And use that state in input's value tag. Like this:

<input value={state.name} ...other_attributes /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM