8

I have a React component that will render a table based on data from an Ajax call. There might just be one row of data, or there might be a maximum of N rows(not a huge amount, but there's a limit).

Using react-bootstrap, how would I create a table depending on the data I get from this.state.data?

Here's some sample code from render of the class. How would I populate the individual cells depending on the length of my array?

The array contains an object for each row I wish to populate, with data corrosponding to Name and Address as well as Age(just an example). How do I populate the <td>'s with the right amount? It might be anywhere from 1 to N.

render : function () {


        let personArray = []; // Array containing objects that each match the three columns I wish to populate. 


        for(let i = 0; i < personArray.length; i++){
            console.log(personArray[i]); // Prints correctly each object with three value key pairs inside
        }

        const popoverLeft = (

            <Popover id="popover-positioned-left" title="Country name">
                <Table striped bordered condensed hover>
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Address</th>
                        <th>Age</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        // Goes on .. 
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        // Goes on .. 
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        // Goes on .. 
                    </tr>
                    </tbody>
                </Table>
            </Popover>
        );

        return(
            <div>
                <OverlayTrigger trigger="click" placement="right" overlay={popoverLeft}>
                    <div>
                    </div>
                </OverlayTrigger>
            </div>
        )

1 Answer 1

12

You can do something like this :

In your render :

<Table striped condensed hover>
  <thead>
    <tr>
      <th></th>
      <th>Name</th>
      <th>Address</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    {personArray.map(this.renderPerson)}
  </tbody>
</Table>

Your renderPerson function :

renderPerson(person, index) {
  return (
    <tr key={index}>
      <td>{person.name}</td>
      <td>{person.address}</td>
      <td>{person.age}</td>
    </tr>
  )
}

The map function will iterate over your array and return a new array of elements to render.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, will try this. Shouldn't renderPerson be function renderPerson in your example, though?
I wrote this using ES6 syntax so you're right, you'll have to tweak it for your syntax needs :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.