如何将数组动态添加到 react-bootstrap 表?

cbl*_*bll 8 javascript arrays reactjs react-bootstrap

我有一个 React 组件,它将根据来自 Ajax 调用的数据呈现一个表。可能只有一行数据,也可能最多有 N 行(数量不多,但有限制)。

使用react-bootstrap,我将如何根据我从中获得的数据创建一个表this.state.data

render是该课程的一些示例代码。我将如何根据数组的长度填充单个单元格?

该数组包含我希望填充的每一行的对象,数据对应于NameAddress以及Age(只是一个示例)。我如何<td>用正确的数量填充's ?它可能是从 1 到 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>
        )
Run Code Online (Sandbox Code Playgroud)

Cla*_*fou 10

你可以这样做:

在你的渲染中:

<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>
Run Code Online (Sandbox Code Playgroud)

您的 renderPerson 功能:

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

地图功能将遍历阵列,并且返回元素来呈现的一个新的数组。