【发布时间】:2018-07-03 20:43:45
【问题描述】:
我使用 ReactJS、NodeJS、MongoDB 制作了板球网络应用程序。在前端,我每页显示来自 JSON 对象的数据,我想显示 16 个 JSON 对象。我正在使用.slice(0,16) 方法从返回 577 个对象的 REST API 中分割 JSON 对象。现在如何实现第 2、3、4、5 页的分页。
对于第 2 页 -> 切片 (17,33) 对于第 3 页 -> 切片(34,49) 等等……
以下是我的 webapp 的代码和屏幕截图:
在 content.js 中:
import React, { Component } from 'react';
import './content.css';
class Content extends Component {
constructor(props){
super(props);
this.state = {
matches:[],
loading:true
};
}
componentDidMount(){
fetch('api/matches')
.then(res => res.json())
.then(res => {
console.log(res)
this.setState({
matches:res.slice(0,16),
loading:false
})
})
}
renderMatches() {
return this.state.matches.map(match => {
return (
<div class="col-lg-3">
<div id="content">
<p class="match">MATCH {match.id}</p>
<h4>{match.team1}</h4>
<p>VS</p>
<h4>{match.team2}</h4>
<div class="winner">
<h3>WINNER</h3>
<h4>{match.winner}</h4>
</div>
<div class="stats">
<button type="button" class="btn btn-success">View Stats</button>
</div>
</div>
</div>
);
})
}
render() {
if (this.state.loading) {
return <div>>Loading...</div>
}
return (
<div>
<div class="row">
{this.renderMatches()}
</div>
</div>
);
}
}
export default Content;
在 Pagination.js 中:
import React, { Component } from 'react';
class Pagination extends Component {
render() {
return (
<div>
<div class="container">
<h2>Pagination</h2>
<p>The .pagination class provides pagination links:</p>
<ul class="pagination">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
</ul>
</div>
</div>
);
}
}
export default Pagination;
截图更清晰: enter image description here
【问题讨论】:
标签: javascript json reactjs pagination