Skip to content

Commit

Permalink
feature: [Table] Pagination on the top in addition to the bottom. fix… (
Browse files Browse the repository at this point in the history
#9357)

* feature: [Table] Pagination on the top in addition to the bottom. fix (#9346)

* refactor pagination of table (#9357)

* specify the position of pagination when using table  - add test

* specify the position of pagination when using table - update API docs

* update api docs

* fix error

* add TablePaginationConfig extends PaginationProps
  • Loading branch information
kanweiwei authored and yesmeck committed Mar 2, 2018
1 parent bea95bf commit ae2990e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
15 changes: 9 additions & 6 deletions components/table/Table.tsx
Expand Up @@ -3,7 +3,7 @@ import * as ReactDOM from 'react-dom';
import RcTable from 'rc-table';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Pagination, { PaginationProps } from '../pagination';
import Pagination from '../pagination';
import Icon from '../icon';
import Spin from '../spin';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
Expand All @@ -28,6 +28,7 @@ import {
CompareFn,
TableStateFilters,
SelectionItemSelectFn,
TablePaginationConfig,
} from './interface';

function noop() {
Expand Down Expand Up @@ -147,7 +148,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}

getDefaultPagination(props: TableProps<T>) {
const pagination: PaginationProps = props.pagination || {};
const pagination: TablePaginationConfig = props.pagination || {};
return this.hasPagination(props) ?
{
...defaultPagination,
Expand Down Expand Up @@ -778,7 +779,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
}
}

renderPagination() {
renderPagination(paginationPosition: string) {
// 强制不需要分页
if (!this.hasPagination()) {
return null;
Expand All @@ -790,10 +791,11 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
} else if (this.props.size as string === 'middle' || this.props.size === 'small') {
size = 'small';
}
let position = pagination.position || 'bottom';
let total = pagination.total || this.getLocalData().length;
return (total > 0) ? (
return (total > 0 && (position === paginationPosition || position === 'both')) ? (
<Pagination
key="pagination"
key={`pagination-${paginationPosition}`}
{...pagination}
className={classNames(pagination.className, `${this.props.prefixCls}-pagination`)}
onChange={this.handlePageChange}
Expand Down Expand Up @@ -993,8 +995,9 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
{...loading}
className={loading.spinning ? `${paginationPatchClass} ${prefixCls}-spin-holder` : ''}
>
{this.renderPagination('top')}
{table}
{this.renderPagination()}
{this.renderPagination('bottom')}
</Spin>
</div>
);
Expand Down
13 changes: 13 additions & 0 deletions components/table/__tests__/Table.pagination.test.js
Expand Up @@ -131,4 +131,17 @@ describe('Table.pagination', () => {
wrapper.setProps({ dataSource: [data[0]] });
expect(wrapper.find('.ant-pagination-item-1').hasClass('ant-pagination-item-active')).toBe(true);
});

it('specify the position of pagination', () => {
const wrapper = mount(createTable({ pagination: { position: 'top' } }));
expect(wrapper.find('.ant-spin-container').children()).toHaveLength(2);
expect(wrapper.find('.ant-spin-container').childAt(0).find('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { position: 'bottom' } });
expect(wrapper.find('.ant-spin-container').children()).toHaveLength(2);
expect(wrapper.find('.ant-spin-container').childAt(1).find('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { position: 'both' } });
expect(wrapper.find('.ant-spin-container').children()).toHaveLength(3);
expect(wrapper.find('.ant-spin-container').childAt(0).find('.ant-pagination')).toHaveLength(1);
expect(wrapper.find('.ant-spin-container').childAt(2).find('.ant-pagination')).toHaveLength(1);
});
});
12 changes: 11 additions & 1 deletion components/table/index.en-US.md
Expand Up @@ -65,7 +65,7 @@ const columns = [{
| indentSize | Indent size in pixels of tree data | number | 15 |
| loading | Loading status of table | boolean\|[object](https://ant.design/components/spin-cn/#API) ([more](https://github.com/ant-design/ant-design/issues/4544#issuecomment-271533135)) | `false` |
| locale | i18n text including filter, sort, empty text, etc | object | filterConfirm: 'Ok' <br> filterReset: 'Reset' <br> emptyText: 'No Data' <br> [Default](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) |
| pagination | Pagination [config](/components/pagination/), hide it by setting it to `false` | object | |
| pagination | Pagination [config](#pagination) or [`Pagination`] (/components/pagination/), hide it by setting it to `false` | object | |
| rowClassName | Row's className | Function(record, index):string | - |
| rowKey | Row's unique key, could be a string or function that returns a string | string\|Function(record):string | `key` |
| rowSelection | Row selection [config](#rowSelection) | object | null |
Expand Down Expand Up @@ -135,6 +135,16 @@ One of the Table `columns` prop for describing the table's columns, Column has t
| -------- | ----------- | ---- | ------- |
| title | Title of the column group | string\|ReactNode | - |
### pagination
Properties for pagination.
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| position | specify the position of `Pagination` | 'top' \| 'bottom' \| 'both' | 'bottom' |
More about pagination, please check [`Pagination`](/components/pagination/).
### rowSelection
Properties for row selection.
Expand Down
12 changes: 11 additions & 1 deletion components/table/index.zh-CN.md
Expand Up @@ -66,7 +66,7 @@ const columns = [{
| indentSize | 展示树形数据时,每层缩进的宽度,以 px 为单位 | number | 15 |
| loading | 页面是否加载中 | boolean\|[object](https://ant.design/components/spin-cn/#API) ([更多](https://github.com/ant-design/ant-design/issues/4544#issuecomment-271533135)) | false |
| locale | 默认文案设置,目前包括排序、过滤、空数据文案 | object | filterConfirm: '确定' <br> filterReset: '重置' <br> emptyText: '暂无数据' <br> [默认值](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) |
| pagination | 分页器,配置项参考 [pagination](/components/pagination/),设为 false 时不展示和进行分页 | object | |
| pagination | 分页器,参考[配置项](#pagination) [pagination](/components/pagination/),设为 false 时不展示和进行分页 | object | |
| rowClassName | 表格行的类名 | Function(record, index):string | - |
| rowKey | 表格行 key 的取值,可以是字符串或一个函数 | string\|Function(record):string | 'key' |
| rowSelection | 列表项是否可选择,[配置项](#rowSelection) | object | null |
Expand Down Expand Up @@ -136,6 +136,16 @@ const columns = [{
| --- | --- | --- | --- |
| title | 列头显示文字 | string\|ReactNode | - |
### pagination
分页的配置项。
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| position | 指定分页显示的位置 | 'top' \| 'bottom' \| 'both' | 'bottom' |
更多配置项,请查看 [`Pagination`](/components/pagination/)。
### rowSelection
选择功能的配置。
Expand Down
10 changes: 7 additions & 3 deletions components/table/interface.tsx
Expand Up @@ -58,6 +58,10 @@ export interface TableLocale {
export type RowSelectionType = 'checkbox' | 'radio';
export type SelectionSelectFn<T> = (record: T, selected: boolean, selectedRows: Object[]) => any;

export interface TablePaginationConfig extends PaginationProps {
position?: 'top' | 'bottom' | 'both';
}

export interface TableRowSelection<T> {
type?: RowSelectionType;
selectedRowKeys?: string[] | number[];
Expand All @@ -75,7 +79,7 @@ export interface TableProps<T> {
prefixCls?: string;
dropdownPrefixCls?: string;
rowSelection?: TableRowSelection<T>;
pagination?: PaginationProps | false;
pagination?: TablePaginationConfig | false;
size?: 'default' | 'middle' | 'small';
dataSource?: T[];
components?: TableComponents;
Expand All @@ -91,7 +95,7 @@ export interface TableProps<T> {
expandRowByClick?: boolean;
onExpandedRowsChange?: (expandedRowKeys: string[] | number[]) => void;
onExpand?: (expanded: boolean, record: T) => void;
onChange?: (pagination: PaginationProps | boolean, filters: string[], sorter: Object) => any;
onChange?: (pagination: TablePaginationConfig | boolean, filters: string[], sorter: Object) => any;
loading?: boolean | SpinProps;
locale?: Object;
indentSize?: number;
Expand All @@ -115,7 +119,7 @@ export interface TableStateFilters {
}

export interface TableState<T> {
pagination: PaginationProps;
pagination: TablePaginationConfig;
filters: TableStateFilters;
sortColumn: ColumnProps<T> | null;
sortOrder: string;
Expand Down
1 change: 1 addition & 0 deletions components/table/style/index.less
Expand Up @@ -12,6 +12,7 @@
.reset-component;
position: relative;
border-radius: @border-radius-base @border-radius-base 0 0;
clear: both;

&-body {
transition: opacity .3s;
Expand Down

1 comment on commit ae2990e

@afc163
Copy link
Member

@afc163 afc163 commented on ae2990e Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.