antd中分页器如何自定义_react+antd分页 实现分页及页面刷新时回到刷新前的page...

本文介绍了如何在React应用中使用Ant Design的分页组件进行自定义,确保页面刷新时能回到之前的状态。通过设置`window.location.hash`来保存和恢复分页状态,并在`componentDidMount`中手动调用`onPageChange`方法。代码示例展示了分页器的配置和事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import React from 'react'import { Pagination, Spin } from'antd' //引入分页组件import'whatwg-fetch'import'./agricultural.less'class Agricultural extends React.Component {

constructor(props) {

super(props)this.state ={

agriculturalList: [],

currentData: [],

total:0,

pageSize:3,

pageNumber: parseInt(window.location.hash.slice(1), 0) || 1 //获取当前页面的hash值,转换为number类型

}//在react中点击事件里面 setState 时会使this重新定义,所以在点击的函数里面使用this.setState()时会报错this.setState not a function,因此需要提前给点击事件的函数绑定this

this.onPageChange = this.onPageChange.bind(this);this.createMarkup = this.createMarkup.bind(this);

}

componentDidMount() {this.handleAnchor() //页面刷新时回到刷新前的page}

handleAnchor() {

this.onPageChange(this.state.pageNumber, this.state.pageSize); //手动调用onPageChange,传入当前页数和每页条数

}onPageChange(page, pageSize) {this.setState({

pageNumber: page

}, ()=>{

window.location.hash = `#${page}`; //设置当前页面的hash值为当前page页数

})

}render() {

const agriculturalListData= this.state.currentData;return(

// 你要显示的数据内容

//分页实现

className="pagination-com"

showQuickJumper

hideOnSinglePage={ true }

defaultCurrent={ this.state.pageNumber }

current={ this.state.pageNumber }

total={ this.state.total }

pageSize={ this.state.pageSize }

onChange={ this.onPageChange }

showTotal={ (e) => {

return "Total " + e + " items"

} } />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值