react分页效果

这篇博客介绍了如何在React应用中实现分页效果,包括创建分页按钮、处理页码变化和数据请求。作者展示了用于控制分页状态的方法,如`makePage`、`handlePage`和`normalClick`,并提供了分页逻辑的详细实现。

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

很逗比的时候写的分页,更逗比的时候发出来,
const numb = {
    firstPage:1,
    currentPage:null,
    totalPage:null,
    pageNumber:null,
};
 
this.state = {
    //分页相关
    firstPage:1,
    currentPage:1,
    showNumber:5,
}
//分页控制
 //创建分页
this.makePage = this.makePage.bind(this);
 //常规点击
this.normalClick = this.normalClick.bind(this);
 //尾页
this.endPage = this.endPage.bind(this);
 //首页
this.firstPage = this.firstPage.bind(this);
 //上一页
this.prevePage = this.prevePage.bind(this);
 //下一页
this.nextPage = this.nextPage.bind(this);
 //计算要显示的页
this.handlePage = this.handlePage.bind(this);
 //handlePage里的过度方法
this.makeBTN = this.makeBTN.bind(this);
 //点击请求数据
this.handPageData = this.handPageData.bind(this);
 //分页逻辑控制
    makeBTN(i,b){
        const anniu = [
            <RaisedButton  label={ i} onTouchTap = { i<=numb.totalPage ?b :null}  primary = {i===this.state.currentPage? true:false} style = {styles.normalBTN}/>,
    ]
        return anniu;
    }
    makePage(){
        const page = [
            <div style ={styles.needCenter} >
    <RaisedButton  label="首页" onTouchTap = {!(this.state.currentPage===1)?this.firstPage:null} style={styles.bottonBTN}/>
    <RaisedButton  label="上一页" onTouchTap = {!(this.state.currentPage===1)? this.prevePage:null} style={styles.bottonBTN}/>
        {this.handlePage()}
    <RaisedButton  label="下一页" onTouchTap = {!(this.state.currentPage===numb.totalPage|| numb.totalPage===0)? this.nextPage:null} style={styles.bottonBTN}/>
    <RaisedButton  label="尾页" onTouchTap = {!(this.state.currentPage===numb.totalPage)?this.endPage:null} style={styles.bottonBTN}/>
    <RaisedButton  label={(this.state.currentPage===1 | this.state.currentPage===numb.totalPage )? "共"+numb.totalPage+"页":this.state.currentPage+'/'+numb.totalPage} style={styles.bottonBTN}/>
    </div>,
    ]
        return page;
    }
    handPageData(a,b,c,d){

    }
    handlePage(){
        numb.totalNumber = this.store._number;
        console.log(numb.totalNumber,"00000000011111111111");
        //数组个数求多少个页面
        let n = numb.totalNumber;
        //设定实验数组值
        //根据数组值算出页面数
        //这个值有可能是浮点型
        // let pp = n/numb.showNum;
        let pp = n/this.state.showNumber;
        //如果是浮点型,取最大的整型值
        let p =Math.ceil(pp);
        numb.totalPage = p;
        //如果页面的求值为整型就直接返回,不是得话,就返回最大的整型
        let ap = pp === 0 ? pp : p ;
        //最终的页数,只能展示10个页面
        let fp = p <10 ? p :10;
        numb.pageNumber = fp;
        let arr = new Array();
        for(let i = this.state.firstPage ; i <= this.state.firstPage+fp-1;i++){
            const num = [this.makeBTN(i,  this.normalClick.bind(null,i))] ;
            arr.push(num);
        }
        return arr;
    }
    endPage(){
        let ctx = this;
        numb.firstPage = numb.totalPage - numb.pageNumber+1;
        ctx.setState(
            {
                firstPage:numb.totalPage - numb.pageNumber+1,
                currentPage:numb.totalPage,
            },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
    );
    }
    firstPage(){
        let ctx = this;
        ctx.setState(
            {
                firstPage:1,
                currentPage:1,
            },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
    );
    }
    nextPage(){
        if(this.state.currentPage < numb.totalPage - numb.pageNumber){
            let ctx = this;
            numb.currentPage =  ++this.state.currentPage;
            numb.firstPage=numb.currentPage;
            ctx.setState(
                {
                    firstPage:numb.firstPage,
                    currentPage:numb.currentPage,
                },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
        );
        }else{
            let ctx = this;
            numb.currentPage =  ++this.state.currentPage;
            numb.firstPage=numb.currentPage;
            ctx.setState(
                {
                    firstPage:numb.totalPage - numb.pageNumber+1,
                    currentPage:numb.currentPage,
                },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
        );
       }
    }
    prevePage(){
        if(this.state.currentPage>numb.pageNumber){
            if(this.state.currentPage>numb.totalPage - numb.pageNumber+1){
                numb.firstPage--;
                let ctx = this;
                numb.currentPage =  this.state.currentPage-1;
                ctx.setState(
                    {
                        firstPage:numb.totalPage - numb.pageNumber+1,
                        currentPage:numb.currentPage,
                    },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
            );}else{
                numb.firstPage--;
                let ctx = this;
                numb.currentPage =  this.state.currentPage-1;
                ctx.setState(
                    {
                        firstPage:numb.currentPage,
                        currentPage:numb.currentPage,
                    },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
            )
            }
        }else{
            numb.firstPage--;
            let ctx = this;
            numb.currentPage =  this.state.currentPage-1;
            ctx.setState(
                {
                    firstPage:1,
                    currentPage:numb.currentPage,
                },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
        );
        }
    }
    normalClick(i)
    {
        if(i<=numb.totalPage - numb.pageNumber){
            let ctx = this;
            numb.firstPage =i;
            numb.currentPage = this.state.currentPage;
            ctx.setState(
                {
                    firstPage:i,
                    currentPage:i,
                },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}
        );
        }else{
            let ctx = this;分页视频2            numb.firstPage =numb.totalPage - numb.pageNumber+1;
            numb.currentPage = this.state.currentPage;
            ctx.setState(
                {
                    firstPage:numb.totalPage - numb.pageNumber+1,
                    currentPage:i,
                },()=>{this.handPageData(this.state.currentPage,this.state.showNumber,this.state.stage,this.state.keyWord)}

        );
        }
    }

//分页
因为pgAdmin数据比较少,就分别用每页展示一行数据和三行数据记录了下,附上效果视频分页效果1分页效果2。秉着一颗学徒的心。。。

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值