Pagination分页

采用分页的形式分隔长列表,每次只加载一个页面。

何时使用#

  • 当加载/渲染所有数据将花费很多时间时;

  • 可切换页码浏览数据。

代码演示

基础分页。

expand codeexpand code
import { Pagination } from 'antd';
import React from 'react';

const App: React.FC = () => <Pagination defaultCurrent={1} total={50} />;

export default App;

更多分页。

expand codeexpand code
import { Pagination } from 'antd';
import React from 'react';

const App: React.FC = () => <Pagination defaultCurrent={6} total={500} />;

export default App;

改变每页显示条目数。

expand codeexpand code
import type { PaginationProps } from 'antd';
import { Pagination } from 'antd';
import React from 'react';

const onShowSizeChange: PaginationProps['onShowSizeChange'] = (current, pageSize) => {
  console.log(current, pageSize);
};

const App: React.FC = () => (
  <>
    <Pagination
      showSizeChanger
      onShowSizeChange={onShowSizeChange}
      defaultCurrent={3}
      total={500}
    />
    <br />
    <Pagination
      showSizeChanger
      onShowSizeChange={onShowSizeChange}
      defaultCurrent={3}
      total={500}
      disabled
    />
  </>
);

export default App;

快速跳转到某一页。

expand codeexpand code
import type { PaginationProps } from 'antd';
import { Pagination } from 'antd';
import React from 'react';

const onChange: PaginationProps['onChange'] = pageNumber => {
  console.log('Page: ', pageNumber);
};

const App: React.FC = () => (
  <>
    <Pagination showQuickJumper defaultCurrent={2} total={500} onChange={onChange} />
    <br />
    <Pagination showQuickJumper defaultCurrent={2} total={500} onChange={onChange} disabled />
  </>
);

export default App;
  • Total 50 items
  • 1
  • 2
  • 3
  • 4
  • 5
  • 10 条/页
    跳至

迷你版本。

expand codeexpand code
import type { PaginationProps } from 'antd';
import { Pagination } from 'antd';
import React from 'react';

const showTotal: PaginationProps['showTotal'] = total => `Total ${total} items`;

const App: React.FC = () => (
  <>
    <Pagination size="small" total={50} />
    <Pagination size="small" total={50} showSizeChanger showQuickJumper />
    <Pagination size="small" total={50} showTotal={showTotal} />
    <Pagination
      size="small"
      total={50}
      disabled
      showTotal={showTotal}
      showSizeChanger
      showQuickJumper
    />
  </>
);

export default App;
  • /5

  • /5

简单的翻页。

expand codeexpand code
import { Pagination } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Pagination simple defaultCurrent={2} total={50} />
    <br />
    <Pagination disabled simple defaultCurrent={2} total={50} />
  </>
);

export default App;

受控制的页码。

expand codeexpand code
import type { PaginationProps } from 'antd';
import { Pagination } from 'antd';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [current, setCurrent] = useState(3);

  const onChange: PaginationProps['onChange'] = page => {
    console.log(page);
    setCurrent(page);
  };

  return <Pagination current={current} onChange={onChange} total={50} />;
};

export default App;
  • Total 85 items
  • 1
  • 2
  • 3
  • 4
  • 5
  • 20 条/页

  • 1-20 of 85 items
  • 1
  • 2
  • 3
  • 4
  • 5
  • 20 条/页

通过设置 showTotal 展示总共有多少数据。

expand codeexpand code
import { Pagination } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <>
    <Pagination
      total={85}
      showTotal={total => `Total ${total} items`}
      defaultPageSize={20}
      defaultCurrent={1}
    />
    <br />
    <Pagination
      total={85}
      showTotal={(total, range) => `${range[0]}-${range[1]} of ${total} items`}
      defaultPageSize={20}
      defaultCurrent={1}
    />
  </>
);

export default App;

展示所有配置选项。

expand codeexpand code
import { Pagination } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <Pagination
    total={85}
    showSizeChanger
    showQuickJumper
    showTotal={total => `Total ${total} items`}
  />
);

export default App;

修改上一步和下一步为文字链接。

expand codeexpand code
import type { PaginationProps } from 'antd';
import { Pagination } from 'antd';
import React from 'react';

const itemRender: PaginationProps['itemRender'] = (_, type, originalElement) => {
  if (type === 'prev') {
    return <a>Previous</a>;
  }
  if (type === 'next') {
    return <a>Next</a>;
  }
  return originalElement;
};

const App: React.FC = () => <Pagination total={500} itemRender={itemRender} />;

export default App;

API#

<Pagination onChange={onChange} total={50} />
参数说明类型默认值版本
current当前页数number-
defaultCurrent默认的当前页数number1
defaultPageSize默认的每页条数number10
disabled禁用分页boolean-
hideOnSinglePage只有一页时是否隐藏分页器booleanfalse
itemRender用于自定义页码的结构,可用于优化 SEO(page, type: 'page' | 'prev' | 'next', originalElement) => React.ReactNode-
pageSize每页条数number-
pageSizeOptions指定每页可以显示多少条string[][10, 20, 50, 100]
responsive当 size 未指定时,根据屏幕宽度自动调整尺寸boolean-
showLessItems是否显示较少页面内容booleanfalse
showQuickJumper是否可以快速跳转至某页boolean | { goButton: ReactNode }false
showSizeChanger是否展示 pageSize 切换器,当 total 大于 50 时默认为 trueboolean-
showTitle是否显示原生 tooltip 页码提示booleantrue
showTotal用于显示数据总量和当前数据顺序function(total, range)-
simple当添加该属性时,显示为简单分页boolean-
size当为 small 时,是小尺寸分页default | smalldefault
total数据总数number0
onChange页码或 pageSize 改变的回调,参数是改变后的页码及每页条数function(page, pageSize)-
onShowSizeChangepageSize 变化的回调function(current, size)-
PageHeader页头Steps步骤条