【发布时间】:2019-01-09 05:58:23
【问题描述】:
我正在使用react ultimate pagination 来实现分页功能。单击页码时,我正在更新变量以显示需要的内容。问题是我有一个循环可以有可变数量的元素,所以我不知道它们中有多少预先存在。代码如下:
{
this.state.fullAnalysisDone && this.state.activeKey == 3 ?
<div>
{
this.get_analysis_charts(this.chartTypes[parseInt(this.state.activeKey)]).map((chart_arr, idx) => {
return <div style={{visibility: this.state.currPageDiffGenes === (idx + 1) ? 'visible' : 'hidden' }}>
<img src = {chart_arr[0]} style = {{height:"400px", width:"500px"}}/>
<img src = {chart_arr[1]} style = {{height:"400px", width:"500px"}}/>
</div>
})
}
<div style = {{marginLeft: '35%'}}>
<UltimatePaginationBootstrap4 key = 'diff_genes_pagination'
totalPages = {this.get_analysis_charts(this.chartTypes[parseInt(this.state.activeKey)]).length}
onChange = {this.changedDiffGenes}
currentPage = {this.state.currPageDiffGenes}/>
</div>
</div>
: ""
}
我真的不能离开这里<div style={{visibility: this.state.currPageDiffGenes === (idx + 1) ? 'visible' : 'hidden' }}>,因为它占用了页面空间。如果我事先知道迭代次数,我可以执行以下操作:
{
this.state.currPageDiffGenes === 1 ?
loop over but return only the 1st element
:""
}
{
this.state.currPageDiffGenes === 2 ?
loop over but return only the 2nd element
:""
}
...
它会起作用,因为每次都会重新创建元素,但我不能用可变长度循环来做到这一点。我们如何解决这个问题?我在应用程序中使用d3,因此我可以将ids 分配给相应的divs,并且可能在使用分页时破坏插入元素,但我觉得这有点过分,应该有一个更简单的解决方案。
【问题讨论】:
标签: javascript reactjs pagination