[英]Create a div beneath a table row when clicking that row with mapped data
I have a table being populated by rows mapped by an array. 我有一个由数组映射的行填充的表。 I want to have an expanded view (or div) of data when I click a button in that row.
当我单击该行中的按钮时,我想要一个扩展的数据视图(或div)。 I run into an issue where I can't have two table rows in the same .map iteration.
我遇到一个问题,即在同一.map迭代中不能有两个表行。
I'm thinking there are multiple ways of doing this. 我在想有多种方法可以做到这一点。
I could swap a table row for a row that spans the width of the table with a div inside of it, using an if statement tied to the id the of the event I want to expand. 我可以使用一个if语句将一个表行换成一个跨表的行,该行跨越表的宽度并在其中包含一个div,该语句与要扩展的事件的id绑定。
I could jquery a div underneath the table row (not sure how to accomplish this). 我可以在表行的下方查询一个div(不确定如何做到这一点)。
<tbody className={loading}>
{this.state.events.map((event, id) =>
<tr key={event.id}>
<td>{event.data || ' '}</td>
<td>{event.otherData}</td>
<td><TimeStamp time={event.timestamp} /></td>
<td><button onClick={() => this.onShowInfo(event)}>
<i className="fa fa-search-plus" /></button></td>
</tr>
)}
</tbody>
JSX makes it impossible to have two rows rendered in a map function JSX使得无法在map函数中呈现两行
The soulution for your problem can be found here: How to return multiple lines JSX in another return statement in React? 您的问题的解决方案可以在这里找到: 如何在React的另一个return语句中返回多行JSX?
Use this example: 使用此示例:
{[1,2,3].map(function (n) {
return ([
<h3></h3>, // note the comma
<p></p>
]);
})}
Although you're right taht you can't RETURN more than one parent tr
in your logic block, you can have multiple blocks so this would work just fine: 尽管您说对了,但是您不能在逻辑块中返回多个父
tr
,但是您可以有多个块,因此可以正常工作:
<tbody className={loading}>
{this.state.events.map((event, id) =>
<tr key={event.id}>
<td>{event.data || ' '}</td>
<td>{event.otherData}</td>
<td><TimeStamp time={event.timestamp} /></td>
<td><button onClick={() => this.onShowInfo(event)}>
<i className="fa fa-search-plus" /></button></td>
</tr>
)}
{this.returnSomeOtherTr()}
</tbody>
Where returnSomeOtherTr
is a function that returns some other <tr>
. 其中
returnSomeOtherTr
是返回其他<tr>
的函数。
So instead of mapping through the array of events, I pushed each event into an array with a for loop. 因此,我没有通过事件数组进行映射,而是将每个事件通过for循环推入数组。 when the event that I wanted the details of came up, my for loop would push an additional row that occupied the entire span (through colSpan) so that I would have the appropriate space to work with.
当我想要了解详细信息的事件出现时,我的for循环将推送占用整个范围的另一行(通过colSpan),以便我有适当的空间来使用。
eventRows(events){
var eventsArray = []
for (var ev of events) {
eventsArray.push(
<tr key={ev.id}>
</tr>
)
if(ev.id === this.state.showInfo.id) {
eventsArray.push(
<tr>
<td colSpan='5'>
</td>
</tr>
)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.