[英]How to implement pagination on frontend in ReactJS?
I have made cricket web app using ReactJS, NodeJS, MongoDB. 我使用ReactJS,NodeJS,MongoDB制作了板球网络应用程序。 On frontend I am displaying data from JSON objects per page I want to display 16 JSON objects.
在前端,我每页显示来自JSON对象的数据,我想显示16个JSON对象。 I am using
.slice(0,16)
method to slice JSON objects from REST API which returns 577 objects. 我使用
.slice(0,16)
方法从REST API中切割JSON对象,返回577个对象。 Now how can I implement pagination for page 2,3,4,5. 现在我如何实现第2,3,4,5页的分页。
For page 2 -> slice(17,33) For page 3 -> slice(34,49) and so on... 对于第2页 - >切片(17,33)对于第3页 - >切片(34,49)等等...
Below is my code and screenshot of my webapp: 以下是我的webapp的代码和截图:
in content.js : 在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;
In Pagination.js : 在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;
Screenshot for more clarity : enter image description here 屏幕截图更清晰: 在此处输入图像描述
How about store all the matches in the component state and only slice during rendering, based on the page
query string? 如何在组件状态下存储所有匹配项,并在呈现期间仅基于
page
查询字符串进行切片?
In componentDidMount()
, after fetching the data, simply set the state with all the matches: 在
componentDidMount()
,在获取数据之后,只需使用所有匹配项设置状态:
this.setState({
matches: res,
})
Then, given you want to control the pagination with a query string in the url, like: 然后,假设您想要使用URL中的查询字符串来控制分页,例如:
?page=2
You can filter the matches in render( ): 您可以在render( )中过滤匹配项:
const RECORDS_PER_PAGE = 16;
const page = parseInt(getPageNumber(), 10) || 1; // defaults to 1 if no page query specified
const offset = (page - 1) * RECORDS_PER_PAGE
// do your mapping (do this in your renderMapping() method)
const matchComponents = this.state.matches.slice(0 + offset, RECORDS_PER_PAGE + offset).map(/* ... */);
where getPageNumber()
is a method that gives you the query string value for page: 其中
getPageNumber()
是一个为页面提供查询字符串值的方法:
// Use your own query string parser if you wish
getPageNumber() {
return window.location.search // <-- gives you access to the query parameters
.slice(1) // remove the `?`
.split("&")
.reduce((queryHash, queryParam) => {
// e.g. ?page=3 becomes ["page", "3"]
const query = queryParam.split('=');
return Object.assign({}, queryHash, ({
[query[0]]: query[1] || null
}));
}, {}).page;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.