reactjs - How to display 5 page number by default in react
问题描述
I have got series of data that contains some objects in one array(json file) and it will be shown by react.There is a paging which displays all the pages. I want to show only 5 pages by default and when the user click next button the next page (number =6) will be shown. But now when the user is in page (number 5) and click next button nothing happens and we are in page number 5 yet.
class App extends React.Component {
constructor(props){
super(props);
this.state = {
data: [],
.
.
.
currentPage: 1,
itemsPerPage: 25,
startIndex : 0,
endIndex : 5,
};
this.handleClick = this.handleClick.bind(this);
}
render() {
const { data, currentPage, itemsPerPage,startIndex,endIndex } = this.state;
const indexOfLastItem = currentPage * itemsPerPage;
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
const currentItems = data.slice(indexOfFirstItem, indexOfLastItem);
const renderHotel = currentItems.sort((a, b) => a.total - b.total)
.map((item, i) => {
return <div class="item">
<span> {item.name}</span>
</div>
});
const pageNumbers = [];
for (let i = 1; i <= Math.ceil(data.length / itemsPerPage); i++) {
pageNumbers.push(i);
}
const renderPageNumbers = pageNumbers.slice(startIndex, endIndex)
.map(number => {
return (
<li className={(this.state.currentPage === number ? 'active ' : '') + 'controls'}
key={number}
id={number}
onClick={this.handleClick}>
{number}
</li>
);
});
return (
<div>
{renderHotel}
<ul id="page-numbers" class="pagenumDef">
<li onClick={this.decremant}><span>prev</span></li>
{renderPageNumbers}
<li onClick={this.increment}><span>next</span></li>
</ul>
</div>
)
};
increment=(event)=> {
if(this.state.currentPage < this.state.data.length /this.state.itemsPerPage){
this.setState({
currentPage: this.state.currentPage+1
});
}
}
decremant=(event)=> {
if(this.state.currentPage >1){
this.setState({
currentPage: this.state.currentPage-1
});
}}
}
ReactDOM.render(<App/>, document.getElementById('Result'));
解决方案
Where is logic for startIndex and endIndex? I think this could help you:
increment = event => {
if (
this.state.currentPage <
this.state.data.length / this.state.itemsPerPage
) {
if (this.state.currentPage === this.state.endIndex) {
this.setState({
endIndex: this.state.endIndex + 1,
startIndex: this.state.startIndex + 1
});
}
this.setState({
currentPage: this.state.currentPage + 1
});
}
};
decremant = event => {
if (this.state.currentPage > 1) {
if (this.state.currentPage === this.state.startIndex + 1) {
this.setState({
endIndex: this.state.endIndex - 1,
startIndex: this.state.startIndex - 1
});
}
this.setState({
currentPage: this.state.currentPage - 1
});
}
};
Code Sansbox: https://codesandbox.io/s/on911vv05
推荐阅读
- swiftui - 抑制冗长的控制台输出 - SwiftUI / MacOS Monterey
- flutter - 带有标签栏和网格视图的 Flutter sliver 应用栏
- rxjs - 绅士,一个 RxJs 计时器,它等待进程在下一个间隔之前完成执行
- elasticsearch - ETL - 弹性负载指标日志捕获
- angular - 使用 simple-json-replay-server 时出现错误 404 Not found 错误
- android - Webview 中的支付网关
- r - 如何使用 dplyr R 选择名称为日期的列
- oracle - 从 Oracle SQL 日期导入 SQOOP 以 unix 时间格式存储后,如何导出回 ORACLE
- r - ezANOVA 中的错误:一个或多个单元格缺少数据
- .net - NSwag vs Swashbuckle