非受控分页,是指分页组件的状态由自己维护,组件值的改变可以通过 onChange
事件通知父组件,默认值由 defaultCurrent
初始化。
import { Pagination } from '@alifd/next';
const change = function(value) {console.log(value);};ReactDOM.render(<Pagination defaultCurrent={2} onChange={change} />, mountNode);
受控分页,是指分页组件的状态由父组件维护,组件自身只负责渲染其父组件传递的值,父组件通过 current
属性传递当前的值。
import { Pagination } from '@alifd/next';
class Demo extends React.Component {constructor(props) {super(props);this.state = {current: 2};this.handleChange = this.handleChange.bind(this);}handleChange(current) {this.setState({current});}render() {return (<Pagination current={this.state.current} onChange={this.handleChange} />);}}ReactDOM.render(<Demo />, mountNode);
当分页数大于5时,自动展示 ...
import { Pagination } from '@alifd/next';
ReactDOM.render(<Pagination total={500} />, mountNode);
可以通过设置 pageSize
属性来指定每页显示的数量。
可以通过设置 pageSizeSelector
属性来指定是否显示 每页数量选择 的部件以及部件形状。
可以通过设置 pageSizeList
属性来指定 每页显示数量 可选的值。
可以通过设置 pageSizePosition
属性来指定 每页显示数量选择 的部件显示在整个组件的开始位置还是结束位置。
可以通过设置 onPageSizeChange
属性来指定每页显示的数量变化时的回调函数。
import { Pagination } from '@alifd/next';
class Demo extends React.Component {constructor(...args) {super(...args);this.state = {pageSize: 20};this.handleChange = pageSize => {this.setState({ pageSize });};}render() {return (<div><h3>Set page size</h3><Pagination pageSize={20} /><h3>Hide page size selector</h3><Pagination pageSizeSelector={false} /><h3>Set page size selector to 'dropdown',and show it in the end</h3><Pagination pageSizeSelector="dropdown" pageSizePosition="end" /><h3>Use pageSizeList to specify the number of records per page.</h3><PaginationpageSize={this.state.pageSize}total={100}pageSizeSelector="filter"pageSizeList={[5, 10, 20]}onPageSizeChange={this.handleChange}/></div>);}}ReactDOM.render(<Demo />, mountNode);
快速跳转到某一页,可以设置 false
来隐藏。
import { Pagination } from '@alifd/next';
const change = function(value) {console.log(value);};ReactDOM.render(<div><h3>Hide jump forcibly</h3><Pagination total={500} showJump={false} /></div>,mountNode);
可以通过指定 size
属性来设置分页的尺寸。
import { Pagination } from '@alifd/next';
ReactDOM.render(<div><h3>small</h3><Pagination defaultCurrent={2} size="small" /><h3>medium</h3><Pagination defaultCurrent={2} size="medium" /><h3>large</h3><Pagination defaultCurrent={2} size="large" /></div>,mountNode);
可以通过指定 shape
属性来设置前进后退按钮箭头的显示方式。
import { Pagination } from '@alifd/next';
ReactDOM.render(<div><h3>normal</h3><Pagination defaultCurrent={2} /><h3>arrow-only</h3><Pagination defaultCurrent={2} shape="arrow-only" /><h3>arrow-prev-only</h3><Pagination defaultCurrent={2} shape="arrow-prev-only" /><h3>no-border</h3><Pagination defaultCurrent={2} shape="no-border" type="simple" /></div>,mountNode);
.next-pagination + .next-pagination {
margin-top: 20px;
}
可以通过指定 type
属性来设置分页器的类型。
import { Pagination } from '@alifd/next';
ReactDOM.render(<div><h3>normal</h3><Pagination defaultCurrent={2} /><h3>simple</h3><Pagination defaultCurrent={2} type="simple" /><h3>mini</h3><Pagination defaultCurrent={2} type="mini" /></div>,mountNode);
分页组件默认不显示总数,你可以通过 totalRender 自定义总数的显示结果。
import { Pagination } from '@alifd/next';
const total = 50;ReactDOM.render(<PaginationclassName="custom-pagination"total={total}totalRender={total => `Total: ${total}`}/>,mountNode);
.custom-pagination {
display: inline-block;
margin-left: 10px;
}
使用popupProps
prop中的align
属性设置下拉位置。
import { Pagination } from '@alifd/next';
const handlePageSizeChange = size => console.log(size);const containerStyle = {height: "300px",padding: 0,width: "100%"};const boxStyle = {overflow: "auto",position: "relative",width: "100%",height: "200px",border: "1px solid black"};const tempStyle = {height: "200px",display: "flex","justify-content": "center","align-items": "center"};const parentStyle = {display: "flex","justify-content": "space-between"};const popupProps = {align: "bl tl"};ReactDOM.render(<div><h3>default align - 'tl bl'</h3><PaginationpageSizeSelector="dropdown"total="10"pageSizePosition="start"onPageSizeChange={handlePageSizeChange}/><h3>custom align - 'bl tl'</h3><PaginationpageSizeSelector="dropdown"total="10"pageSizePosition="start"onPageSizeChange={handlePageSizeChange}popupProps={popupProps}/><h3>Inside parent with "overlflow: auto"</h3><div style={containerStyle}><div style={boxStyle}><div style={tempStyle}>scroll down to see the example</div><div style={parentStyle}><div><h3>default align - 'tl bl'</h3><PaginationpageSizeSelector="dropdown"total="10"pageSizePosition="start"onPageSizeChange={handlePageSizeChange}/></div><div><h3>custom align - 'bl tl'</h3><PaginationpageSizeSelector="dropdown"total="10"pageSizePosition="start"onPageSizeChange={handlePageSizeChange}popupProps={popupProps}/></div></div></div></div></div>,mountNode);
可以通过指定 link
属性来设置页码按钮的跳转链接,方便 SEO,link 属性的值为一个包含 {page}
的模板字符串,Pagination 组件会将该占位符替换为具体的页码数字。
import { Pagination } from '@alifd/next';
const format = `${window.location.href}#/{page}`;ReactDOM.render(<Pagination defaultCurrent={2} link={format} />, mountNode);
单页应用场景下,Pagination 组件可以使用外部跳转的方法来实现单页内部跳转。
import { Pagination } from '@alifd/next';
import { hashHistory } from 'react-router';
function handleChange(page) {hashHistory.push(page.toString());}ReactDOM.render(<Pagination defaultCurrent={2} onChange={handleChange} />,mountNode);
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
size | 分页组件大小 可选值: 'small', 'medium', 'large' |
Enum | 'medium' |
type | 分页组件类型 可选值: 'normal', 'simple', 'mini' |
Enum | 'normal' |
shape | 前进后退按钮样式 可选值: 'normal', 'arrow-only', 'arrow-prev-only', 'no-border' |
Enum | 'normal' |
current | (受控)当前页码 | Number | - |
defaultCurrent | (非受控)初始页码 | Number | 1 |
onChange | 页码发生改变时的回调函数 签名: Function(current: Number, e: Object) => void 参数: current: {Number} 改变后的页码数 e: {Object} 点击事件对象 |
Function | () => {} |
total | 总记录数 | Number | 100 |
totalRender | 总数的渲染函数 签名: Function(total: Number, range: Array) => void 参数: total: {Number} 总数 range: {Array} 当前数据在总数中的区间 |
Function | - |
pageShowCount | 页码显示的数量,更多的使用...代替 | Number | 5 |
pageSize | 一页中的记录数 | Number | 10 |
pageSizeSelector | 每页显示选择器类型 可选值: false, 'filter', 'dropdown' |
Enum | false |
pageSizeList | 每页显示选择器可选值 | Array<Number>/Array<Object> | [5, 10, 20] |
pageNumberRender | 自定义页码渲染函数,函数作用于页码button以及当前页/总页数的数字渲染 签名: Function(index: Number) => ReactNode 参数: index: {Number} 分页的页码,从1开始 返回值: {ReactNode} 返回渲染结果 |
Function | index => index |
pageSizePosition | 每页显示选择器在组件中的位置 可选值: 'start', 'end' |
Enum | 'start' |
useFloatLayout | 存在每页显示选择器时是否使用浮动布局 | Boolean | false |
onPageSizeChange | 每页显示记录数量改变时的回调函数 签名: Function(pageSize: Number) => void 参数: pageSize: {Number} 改变后的每页显示记录数 |
Function | () => {} |
hideOnlyOnePage | 当分页数为1时,是否隐藏分页器 | Boolean | false |
showJump | type 设置为 normal 时,在页码数超过5页后,会显示跳转输入框与按钮,当设置 showJump 为 false 时,不再显示该跳转区域 | Boolean | true |
link | 设置页码按钮的跳转链接,它的值为一个包含 {page} 的模版字符串,如:https://www.taobao.com/{page} | String | - |
popupProps | 弹层组件属性,透传给Popup | Object | - |
按键 | 说明 |
---|---|
Tab | 切换页数 |
Space | 按下按钮 |
Enter | 按下按钮 |