最近自己在学react,尝试自己弄一个分页组件来练练手
分页效果
在线预览
github地址
效果截图(样式可自行修改):
构建项目
1
| create-react-app react-paging-component
|
分页组件
1.子组件
创建 Pagecomponent.js 文件
核心代码:
初始化值
1 2 3 4 5 6 7 8 9
| constructor(props) { super(props) this.state = { currentPage: 1, groupCount: 5, startPage: 1, totalPage:1 } }
|
动态生成页码函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| 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) { 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 {
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;
}
|
页码点击函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 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) }
|
上一页和下一页点击事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| prePageHandeler() { let {currentPage} = this.state if (--currentPage === 0) { return false } this.pageClick(currentPage) }
nextPageHandeler() { let {currentPage,totalPage} = this.state if (++currentPage > totalPage) { return false } this.pageClick(currentPage) }
|
组件渲染到DOM上
1 2 3 4 5 6 7 8
| render() { const pageList = this.createPage(); return ( <ul className="page-container"> {pageList} </ul> ) }
|
2.父组件
创建 Pagecontainer.js 文件
父组件完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 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
|
至此一个分页组件就开发完了,如有疑问欢迎到我个人博客的该文章下留言,原文地址:戳这里