如何将数组动态添加到 react-bootstrap table?
How do I dynamically add an array to a react-bootstrap table?
我有一个 React 组件,它将根据来自 Ajax 调用的数据呈现 table。可能只有一行数据,也可能最多有N行(数量不是很大,但有限制)。
使用 react-bootstrap
,我如何根据从 this.state.data
获得的数据创建 table?
这是 class 的 render
中的一些示例代码。我将如何根据数组的长度填充各个单元格?
数组包含我希望填充的每一行的对象,数据对应于 Name
和 Address
以及 Age
(仅作为示例)。如何用正确的数量填充 <td>
?它可能是从 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>
)
你可以这样做:
在您的渲染中:
<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>
您的 renderPerson 函数:
renderPerson(person, index) {
return (
<tr key={index}>
<td>{person.name}</td>
<td>{person.address}</td>
<td>{person.age}</td>
</tr>
)
}
map 函数将遍历您的数组和 return 一个要呈现的新元素数组。
我有一个 React 组件,它将根据来自 Ajax 调用的数据呈现 table。可能只有一行数据,也可能最多有N行(数量不是很大,但有限制)。
使用 react-bootstrap
,我如何根据从 this.state.data
获得的数据创建 table?
这是 class 的 render
中的一些示例代码。我将如何根据数组的长度填充各个单元格?
数组包含我希望填充的每一行的对象,数据对应于 Name
和 Address
以及 Age
(仅作为示例)。如何用正确的数量填充 <td>
?它可能是从 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>
)
你可以这样做:
在您的渲染中:
<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>
您的 renderPerson 函数:
renderPerson(person, index) {
return (
<tr key={index}>
<td>{person.name}</td>
<td>{person.address}</td>
<td>{person.age}</td>
</tr>
)
}
map 函数将遍历您的数组和 return 一个要呈现的新元素数组。