嘗試自己動手用react來寫一個分頁組件(小結),react分頁

來源:互聯網
上載者:User

嘗試自己動手用react來寫一個分頁組件(小結),react分頁

本文介紹了嘗試自己動手用react來寫一個分頁組件(小結),分享給大家,具體如下:

分頁效果

線上預覽

github地址

效果(樣式可自行修改):

構建項目

create-react-app react-paging-component

分頁組件

1.子組件

建立 Pagecomponent.js 檔案

核心代碼:

初始化值

 constructor(props) {    super(props)    this.state = {      currentPage: 1, //當前頁碼      groupCount: 5, //頁碼分組,顯示7個頁碼,其餘用省略符號顯示      startPage: 1, //分組開始頁碼      totalPage:1 //總頁數    }  }

動態產生頁碼函數

createPage() {    const {currentPage, groupCount, startPage,totalPage} = this.state;    let pages = []    //上一頁    pages.push(<li className={currentPage === 1 ? "nomore" : null} onClick={this.prePageHandeler.bind(this)}            key={0}>      上一頁</li>)    if (totalPage <= 10) {      /*總頁碼小於等於10時,全部顯示出來*/      for (let i = 1; i <= totalPage; i++) {        pages.push(<li key={i} onClick={this.pageClick.bind(this, i)}                className={currentPage === i ? "activePage" : null}>{i}</li>)      }    } else {      /*總頁碼大於10時,部分顯示*/      //第一頁      pages.push(<li className={currentPage === 1 ? "activePage" : null} key={1}              onClick={this.pageClick.bind(this, 1)}>1</li>)      let pageLength = 0;      if (groupCount + startPage > totalPage) {        pageLength = totalPage      } else {        pageLength = groupCount + startPage;      }      //前面省略符號(噹噹前頁碼比分組的頁碼大時顯示省略符號)      if (currentPage >= groupCount) {        pages.push(<li className="" key={-1}>···</li>)      }      //非第一頁和最後一頁顯示      for (let i = startPage; i < pageLength; i++) {        if (i <= totalPage - 1 && i > 1) {          pages.push(<li className={currentPage === i ? "activePage" : null} key={i}                  onClick={this.pageClick.bind(this, i)}>{i}</li>)        }      }      //後面省略符號      if (totalPage - startPage >= groupCount + 1) {        pages.push(<li className="" key={-2}>···</li>)      }      //最後一頁      pages.push(<li className={currentPage === totalPage ? "activePage" : null} key={totalPage}              onClick={this.pageClick.bind(this, totalPage)}>{totalPage}</li>)    }    //下一頁    pages.push(<li className={currentPage === totalPage ? "nomore" : null}            onClick={this.nextPageHandeler.bind(this)}            key={totalPage + 1}>下一頁</li>)    return pages;  }

頁碼點擊函數:

//頁碼點擊  pageClick(currentPage) {    const {groupCount} = this.state    const getCurrentPage = this.props.pageCallbackFn;    //當 當前頁碼 大於 分組的頁碼 時,使 當前頁 前面 顯示 兩個頁碼    if (currentPage >= groupCount) {      this.setState({        startPage: currentPage - 2,      })    }    if (currentPage < groupCount) {      this.setState({        startPage: 1,      })    }    //第一頁時重新設定分組的起始頁    if (currentPage === 1) {      this.setState({        startPage: 1,      })    }    this.setState({      currentPage    })    //將當前頁碼返回父組件    getCurrentPage(currentPage)  }

上一頁和夏夜點擊事件

//上一頁事件  prePageHandeler() {    let {currentPage} = this.state    if (--currentPage === 0) {      return false    }    this.pageClick(currentPage)  }  //下一頁事件  nextPageHandeler() {    let {currentPage,totalPage} = this.state    // const {totalPage} = this.props.pageConfig;    if (++currentPage > totalPage) {      return false    }    this.pageClick(currentPage)  }

組件渲染到DOM上

render() {    const pageList = this.createPage();    return (      <ul className="page-container">        {pageList}      </ul>    )  }

2.父組件

建立 Pagecontainer.js 檔案

父組件完整代碼

import React, {Component} from 'react'import Pagecomponent from '../components/Pagecomponent'import data from '../mock/tsconfig.json'class Pagecontainer extends Component {  constructor() {    super()    this.state = {      dataList:[],      pageConfig: {        totalPage: data.length //總頁碼      }    }    this.getCurrentPage = this.getCurrentPage.bind(this)  }  getCurrentPage(currentPage) {    this.setState({      dataList:data[currentPage-1].name    })  }  render() {    return (      <div>        <div>          {this.state.dataList}        </div>        <Pagecomponent pageConfig={this.state.pageConfig}                pageCallbackFn={this.getCurrentPage}/>      </div>    )  }}export default Pagecontainer

至此一個分頁組件就開發完了,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.