PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 2, 2022

[FIXED] How can I make each row of my table dynamic when I click on it?

 August 02, 2022     html-table, hyperlink, react-router-dom, reactjs     No comments   

Issue

I have the following table made in Django and I want to do the same but with react.

I want each row in my table to be clickable with a dynamic link pointing to said record.

Example:

<tbody>
  {% for i in inventories %}
  <tr onclick="location.href='/inventory/detail/{{i.id}}'">
    <td>{{i.name}}</td>
  </tr>
  {% endfor %}
</tbody>

React:

<Tbody>
  {companies.map((company, index) => (
    <Tr key={index}>
      <Td>{company.company_id}</Td>
      <Td>{company.social_reason_name}</Td>
      <Td>{company.comercial_name}</Td>
      <Td>{company.alias_name}</Td>
      <Td>{company.rnc}</Td>
    </Tr>
  ))}
</Tbody>

How can I make each Tr row in the table become a dynamic link?


Solution

Import the Link component and wrap the row content. Compute and pass the target path to the Link component's to prop.

Example:

import { Link } from 'react-router-dom';

...

<Tbody>
  {companies.map((company, index) => (
    <Tr key={index}>
      <Link to={`/company/detail/${company.company_id}`}>
        <Td>{company.company_id}</Td>
        <Td>{company.social_reason_name}</Td>
        <Td>{company.comercial_name}</Td>
        <Td>{company.alias_name}</Td>
        <Td>{company.rnc}</Td>
      </Link>
    </Tr>
  ))}
</Tbody>


Answered By - Drew Reese
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing