2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2017-05-24 16:16:42 +08:00
|
|
|
import { findDOMNode } from 'react-dom';
|
2016-06-19 10:56:42 +08:00
|
|
|
import RcTable from 'rc-table';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-06-19 10:56:42 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-17 15:23:25 +08:00
|
|
|
import Pagination, { PaginationProps } from '../pagination';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import Spin, { SpinProps } from '../spin';
|
2017-10-20 14:54:38 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2017-10-26 10:50:18 +08:00
|
|
|
import defaultLocale from '../locale-provider/default';
|
2016-11-01 11:10:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2017-03-17 15:23:25 +08:00
|
|
|
import FilterDropdown from './filterDropdown';
|
2016-11-11 15:26:51 +08:00
|
|
|
import createStore, { Store } from './createStore';
|
|
|
|
import SelectionBox from './SelectionBox';
|
2017-02-23 19:29:47 +08:00
|
|
|
import SelectionCheckboxAll, { SelectionDecorator } from './SelectionCheckboxAll';
|
2016-11-22 10:11:12 +08:00
|
|
|
import Column, { ColumnProps } from './Column';
|
|
|
|
import ColumnGroup from './ColumnGroup';
|
2017-03-17 15:23:25 +08:00
|
|
|
import { flatArray, treeMap, flatFilter, normalizeColumns } from './util';
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopPropagation(e) {
|
|
|
|
e.stopPropagation();
|
2016-06-23 16:00:30 +08:00
|
|
|
if (e.nativeEvent.stopImmediatePropagation) {
|
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultPagination = {
|
|
|
|
onChange: noop,
|
|
|
|
onShowSizeChange: noop,
|
|
|
|
};
|
|
|
|
|
2017-03-14 19:51:28 +08:00
|
|
|
/**
|
|
|
|
* Avoid creating new object, so that parent component's shouldComponentUpdate
|
|
|
|
* can works appropriately。
|
|
|
|
*/
|
|
|
|
const emptyObject = {};
|
|
|
|
|
2017-01-20 16:08:20 +08:00
|
|
|
export type TableColumnConfig<T> = ColumnProps<T>;
|
|
|
|
|
2016-10-26 18:26:26 +08:00
|
|
|
export interface TableRowSelection<T> {
|
2016-08-11 15:38:03 +08:00
|
|
|
type?: 'checkbox' | 'radio';
|
2017-06-13 14:43:19 +08:00
|
|
|
selectedRowKeys?: string[] | number[];
|
|
|
|
onChange?: (selectedRowKeys: string[] | number[], selectedRows: Object[]) => any;
|
2016-10-26 18:26:26 +08:00
|
|
|
getCheckboxProps?: (record: T) => Object;
|
|
|
|
onSelect?: (record: T, selected: boolean, selectedRows: Object[]) => any;
|
2016-08-11 15:38:03 +08:00
|
|
|
onSelectAll?: (selected: boolean, selectedRows: Object[], changeRows: Object[]) => any;
|
2017-02-23 19:29:47 +08:00
|
|
|
onSelectInvert?: (selectedRows: Object[]) => any;
|
2017-03-24 11:44:36 +08:00
|
|
|
selections?: SelectionDecorator[] | boolean;
|
2017-08-27 15:44:22 +08:00
|
|
|
hideDefaultSelections?: boolean;
|
2016-08-11 15:38:03 +08:00
|
|
|
}
|
|
|
|
|
2016-10-26 18:26:26 +08:00
|
|
|
export interface TableProps<T> {
|
2016-08-11 15:38:03 +08:00
|
|
|
prefixCls?: string;
|
2016-09-14 16:18:33 +08:00
|
|
|
dropdownPrefixCls?: string;
|
2016-10-26 18:26:26 +08:00
|
|
|
rowSelection?: TableRowSelection<T>;
|
2016-09-19 10:17:07 +08:00
|
|
|
pagination?: PaginationProps | boolean;
|
2017-02-09 22:43:58 +08:00
|
|
|
size?: 'default' | 'middle' | 'small';
|
2016-10-26 18:26:26 +08:00
|
|
|
dataSource?: T[];
|
2016-11-22 10:11:12 +08:00
|
|
|
columns?: ColumnProps<T>[];
|
2016-10-26 18:26:26 +08:00
|
|
|
rowKey?: string | ((record: T, index: number) => string);
|
|
|
|
rowClassName?: (record: T, index: number) => string;
|
2016-08-11 15:38:03 +08:00
|
|
|
expandedRowRender?: any;
|
2017-06-13 14:43:19 +08:00
|
|
|
defaultExpandedRowKeys?: string[] | number[];
|
|
|
|
expandedRowKeys?: string[] | number[];
|
2016-08-11 15:38:03 +08:00
|
|
|
expandIconAsCell?: boolean;
|
2016-09-20 13:16:58 +08:00
|
|
|
expandIconColumnIndex?: number;
|
2017-06-13 14:43:19 +08:00
|
|
|
onExpandedRowsChange?: (expandedRowKeys: string[] | number[]) => void;
|
2017-02-23 12:51:40 +08:00
|
|
|
onExpand?: (expanded: boolean, record: T) => void;
|
2016-09-19 10:17:07 +08:00
|
|
|
onChange?: (pagination: PaginationProps | boolean, filters: string[], sorter: Object) => any;
|
2017-03-28 15:00:19 +08:00
|
|
|
loading?: boolean | SpinProps;
|
2016-08-11 15:38:03 +08:00
|
|
|
locale?: Object;
|
|
|
|
indentSize?: number;
|
2017-06-21 07:29:46 +08:00
|
|
|
onRowClick?: (record: T, index: number, event: Event) => any;
|
2016-08-11 15:38:03 +08:00
|
|
|
useFixedHeader?: boolean;
|
|
|
|
bordered?: boolean;
|
|
|
|
showHeader?: boolean;
|
|
|
|
footer?: (currentPageData: Object[]) => React.ReactNode;
|
|
|
|
title?: (currentPageData: Object[]) => React.ReactNode;
|
2017-09-17 15:44:01 +08:00
|
|
|
scroll?: { x?: boolean | number | string, y?: boolean | number | string };
|
2016-10-24 16:30:38 +08:00
|
|
|
childrenColumnName?: string;
|
2016-09-19 17:35:17 +08:00
|
|
|
bodyStyle?: React.CSSProperties;
|
2016-10-10 22:38:47 +08:00
|
|
|
className?: string;
|
2016-12-19 15:19:15 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-08-11 15:38:03 +08:00
|
|
|
}
|
|
|
|
|
2016-10-26 18:26:26 +08:00
|
|
|
export default class Table<T> extends React.Component<TableProps<T>, any> {
|
2016-11-22 10:11:12 +08:00
|
|
|
static Column = Column;
|
|
|
|
static ColumnGroup = ColumnGroup;
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
static propTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
dataSource: PropTypes.array,
|
|
|
|
columns: PropTypes.array,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
useFixedHeader: PropTypes.bool,
|
|
|
|
rowSelection: PropTypes.object,
|
|
|
|
className: PropTypes.string,
|
|
|
|
size: PropTypes.string,
|
|
|
|
loading: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.object,
|
2017-02-13 15:48:08 +08:00
|
|
|
]),
|
2017-04-12 04:49:08 +08:00
|
|
|
bordered: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
locale: PropTypes.object,
|
|
|
|
dropdownPrefixCls: PropTypes.string,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
dataSource: [],
|
|
|
|
prefixCls: 'ant-table',
|
|
|
|
useFixedHeader: false,
|
|
|
|
rowSelection: null,
|
|
|
|
className: '',
|
|
|
|
size: 'large',
|
|
|
|
loading: false,
|
|
|
|
bordered: false,
|
|
|
|
indentSize: 20,
|
|
|
|
locale: {},
|
2016-07-26 21:14:13 +08:00
|
|
|
rowKey: 'key',
|
2017-01-20 11:33:27 +08:00
|
|
|
showHeader: true,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
CheckboxPropsCache: Object;
|
2016-11-11 15:26:51 +08:00
|
|
|
store: Store;
|
2016-11-22 10:11:12 +08:00
|
|
|
columns: ColumnProps<T>[];
|
2016-08-11 15:38:03 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2016-09-14 11:42:06 +08:00
|
|
|
warning(
|
|
|
|
!('columnsPageRange' in props || 'columnsPageSize' in props),
|
|
|
|
'`columnsPageRange` and `columnsPageSize` are removed, please use ' +
|
2017-07-19 13:59:53 +08:00
|
|
|
'fixed columns instead, see: https://u.ant.design/fixed-columns.',
|
2016-09-14 11:42:06 +08:00
|
|
|
);
|
|
|
|
|
2016-11-22 10:11:12 +08:00
|
|
|
this.columns = props.columns || normalizeColumns(props.children);
|
|
|
|
|
2017-07-03 16:57:11 +08:00
|
|
|
this.state = {
|
2017-09-01 14:40:29 +08:00
|
|
|
...this.getSortStateFromColumns(),
|
2016-06-19 10:56:42 +08:00
|
|
|
// 减少状态
|
|
|
|
filters: this.getFiltersFromColumns(),
|
2017-01-09 20:49:00 +08:00
|
|
|
pagination: this.getDefaultPagination(props),
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
this.CheckboxPropsCache = {};
|
2016-11-11 15:26:51 +08:00
|
|
|
|
|
|
|
this.store = createStore({
|
|
|
|
selectedRowKeys: (props.rowSelection || {}).selectedRowKeys || [],
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 17:53:08 +08:00
|
|
|
getCheckboxPropsByItem = (item, index) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const { rowSelection = {} } = this.props;
|
|
|
|
if (!rowSelection.getCheckboxProps) {
|
|
|
|
return {};
|
|
|
|
}
|
2016-12-16 17:53:08 +08:00
|
|
|
const key = this.getRecordKey(item, index);
|
2016-06-19 10:56:42 +08:00
|
|
|
// Cache checkboxProps
|
|
|
|
if (!this.CheckboxPropsCache[key]) {
|
|
|
|
this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item);
|
|
|
|
}
|
|
|
|
return this.CheckboxPropsCache[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultSelection() {
|
|
|
|
const { rowSelection = {} } = this.props;
|
|
|
|
if (!rowSelection.getCheckboxProps) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-08-03 21:27:45 +08:00
|
|
|
return this.getFlatData()
|
2016-12-16 17:53:08 +08:00
|
|
|
.filter((item, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).defaultChecked)
|
2016-06-19 10:56:42 +08:00
|
|
|
.map((record, rowIndex) => this.getRecordKey(record, rowIndex));
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:49:00 +08:00
|
|
|
getDefaultPagination(props) {
|
|
|
|
const pagination = props.pagination || {};
|
|
|
|
return this.hasPagination(props) ?
|
2017-07-03 16:57:11 +08:00
|
|
|
{
|
|
|
|
...defaultPagination,
|
|
|
|
...pagination,
|
2017-01-09 20:49:00 +08:00
|
|
|
current: pagination.defaultCurrent || pagination.current || 1,
|
|
|
|
pageSize: pagination.defaultPageSize || pagination.pageSize || 10,
|
2017-07-03 16:57:11 +08:00
|
|
|
} : {};
|
2017-01-09 20:49:00 +08:00
|
|
|
}
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-02-08 14:43:00 +08:00
|
|
|
this.columns = nextProps.columns || normalizeColumns(nextProps.children);
|
2017-02-07 21:09:51 +08:00
|
|
|
if ('pagination' in nextProps || 'pagination' in this.props) {
|
2016-06-19 10:56:42 +08:00
|
|
|
this.setState(previousState => {
|
2017-07-03 16:57:11 +08:00
|
|
|
const newPagination = {
|
|
|
|
...defaultPagination,
|
|
|
|
...previousState.pagination,
|
|
|
|
...nextProps.pagination,
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
newPagination.current = newPagination.current || 1;
|
2017-01-09 20:49:00 +08:00
|
|
|
newPagination.pageSize = newPagination.pageSize || 10;
|
2017-03-14 19:51:28 +08:00
|
|
|
return { pagination: nextProps.pagination !== false ? newPagination : emptyObject };
|
2017-01-09 20:49:00 +08:00
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
if (nextProps.rowSelection &&
|
|
|
|
'selectedRowKeys' in nextProps.rowSelection) {
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectedRowKeys: nextProps.rowSelection.selectedRowKeys || [],
|
|
|
|
});
|
2016-06-21 19:35:29 +08:00
|
|
|
const { rowSelection } = this.props;
|
|
|
|
if (rowSelection && (
|
|
|
|
nextProps.rowSelection.getCheckboxProps !== rowSelection.getCheckboxProps
|
|
|
|
)) {
|
2016-06-19 10:56:42 +08:00
|
|
|
this.CheckboxPropsCache = {};
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 21:10:06 +08:00
|
|
|
if ('dataSource' in nextProps &&
|
|
|
|
nextProps.dataSource !== this.props.dataSource) {
|
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
|
|
|
this.CheckboxPropsCache = {};
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
|
2017-02-08 14:43:00 +08:00
|
|
|
if (this.getSortOrderColumns(this.columns).length > 0) {
|
|
|
|
const sortState = this.getSortStateFromColumns(this.columns);
|
2016-06-19 10:56:42 +08:00
|
|
|
if (sortState.sortColumn !== this.state.sortColumn ||
|
|
|
|
sortState.sortOrder !== this.state.sortOrder) {
|
|
|
|
this.setState(sortState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:43:00 +08:00
|
|
|
const filteredValueColumns = this.getFilteredValueColumns(this.columns);
|
2016-06-19 10:56:42 +08:00
|
|
|
if (filteredValueColumns.length > 0) {
|
2017-02-08 14:43:00 +08:00
|
|
|
const filtersFromColumns = this.getFiltersFromColumns(this.columns);
|
2017-07-03 16:57:11 +08:00
|
|
|
const newFilters = { ...this.state.filters };
|
2016-06-19 10:56:42 +08:00
|
|
|
Object.keys(filtersFromColumns).forEach(key => {
|
|
|
|
newFilters[key] = filtersFromColumns[key];
|
|
|
|
});
|
|
|
|
if (this.isFiltersChanged(newFilters)) {
|
|
|
|
this.setState({ filters: newFilters });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
setSelectedRowKeys(selectedRowKeys, { selectWay, record, checked, changeRowKeys }: any) {
|
2016-08-03 21:08:13 +08:00
|
|
|
const { rowSelection = {} } = this.props;
|
|
|
|
if (rowSelection && !('selectedRowKeys' in rowSelection)) {
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({ selectedRowKeys });
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2016-08-03 21:27:45 +08:00
|
|
|
const data = this.getFlatData();
|
2016-08-03 21:08:13 +08:00
|
|
|
if (!rowSelection.onChange && !rowSelection[selectWay]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const selectedRows = data.filter(
|
2017-03-23 21:15:49 +08:00
|
|
|
(row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2016-08-03 21:08:13 +08:00
|
|
|
);
|
|
|
|
if (rowSelection.onChange) {
|
|
|
|
rowSelection.onChange(selectedRowKeys, selectedRows);
|
|
|
|
}
|
|
|
|
if (selectWay === 'onSelect' && rowSelection.onSelect) {
|
|
|
|
rowSelection.onSelect(record, checked, selectedRows);
|
|
|
|
} else if (selectWay === 'onSelectAll' && rowSelection.onSelectAll) {
|
|
|
|
const changeRows = data.filter(
|
2017-03-23 21:15:49 +08:00
|
|
|
(row, i) => changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2016-06-19 10:56:42 +08:00
|
|
|
);
|
2016-08-03 21:08:13 +08:00
|
|
|
rowSelection.onSelectAll(checked, selectedRows, changeRows);
|
2017-02-23 19:29:47 +08:00
|
|
|
} else if (selectWay === 'onSelectInvert' && rowSelection.onSelectInvert) {
|
|
|
|
rowSelection.onSelectInvert(selectedRowKeys);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:49:00 +08:00
|
|
|
hasPagination(props?: any) {
|
|
|
|
return (props || this.props).pagination !== false;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
isFiltersChanged(filters) {
|
|
|
|
let filtersChanged = false;
|
|
|
|
if (Object.keys(filters).length !== Object.keys(this.state.filters).length) {
|
|
|
|
filtersChanged = true;
|
|
|
|
} else {
|
|
|
|
Object.keys(filters).forEach(columnKey => {
|
|
|
|
if (filters[columnKey] !== this.state.filters[columnKey]) {
|
|
|
|
filtersChanged = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return filtersChanged;
|
|
|
|
}
|
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
getSortOrderColumns(columns?) {
|
2017-03-10 14:52:28 +08:00
|
|
|
return flatFilter(columns || this.columns || [], column => 'sortOrder' in column);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
getFilteredValueColumns(columns?) {
|
2017-03-10 14:52:28 +08:00
|
|
|
return flatFilter(columns || this.columns || [], column => typeof column.filteredValue !== 'undefined');
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
getFiltersFromColumns(columns?) {
|
2016-06-19 10:56:42 +08:00
|
|
|
let filters = {};
|
|
|
|
this.getFilteredValueColumns(columns).forEach(col => {
|
|
|
|
filters[this.getColumnKey(col)] = col.filteredValue;
|
|
|
|
});
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
getSortStateFromColumns(columns?) {
|
2017-09-01 14:40:29 +08:00
|
|
|
// return fisrt column which sortOrder is not falsy
|
2016-06-19 10:56:42 +08:00
|
|
|
const sortedColumn =
|
|
|
|
this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0];
|
|
|
|
if (sortedColumn) {
|
|
|
|
return {
|
|
|
|
sortColumn: sortedColumn,
|
|
|
|
sortOrder: sortedColumn.sortOrder,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
sortColumn: null,
|
|
|
|
sortOrder: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getSorterFn() {
|
|
|
|
const { sortOrder, sortColumn } = this.state;
|
|
|
|
if (!sortOrder || !sortColumn ||
|
|
|
|
typeof sortColumn.sorter !== 'function') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return (a, b) => {
|
2016-08-23 18:00:01 +08:00
|
|
|
const result = sortColumn.sorter(a, b);
|
2016-06-19 10:56:42 +08:00
|
|
|
if (result !== 0) {
|
|
|
|
return (sortOrder === 'descend') ? -result : result;
|
|
|
|
}
|
2016-08-23 18:00:01 +08:00
|
|
|
return 0;
|
2016-06-19 10:56:42 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleSortOrder(order, column) {
|
|
|
|
let { sortColumn, sortOrder } = this.state;
|
|
|
|
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
|
|
|
let isSortColumn = this.isSortColumn(column);
|
|
|
|
if (!isSortColumn) { // 当前列未排序
|
|
|
|
sortOrder = order;
|
|
|
|
sortColumn = column;
|
|
|
|
} else { // 当前列已排序
|
|
|
|
if (sortOrder === order) { // 切换为未排序状态
|
|
|
|
sortOrder = '';
|
|
|
|
sortColumn = null;
|
|
|
|
} else { // 切换为排序状态
|
|
|
|
sortOrder = order;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newState = {
|
|
|
|
sortOrder,
|
|
|
|
sortColumn,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Controlled
|
|
|
|
if (this.getSortOrderColumns().length === 0) {
|
|
|
|
this.setState(newState);
|
|
|
|
}
|
|
|
|
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
|
|
|
...newState,
|
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleFilter = (column, nextFilters) => {
|
|
|
|
const props = this.props;
|
2017-07-03 16:57:11 +08:00
|
|
|
let pagination = { ...this.state.pagination };
|
|
|
|
const filters = {
|
|
|
|
...this.state.filters,
|
2016-06-19 10:56:42 +08:00
|
|
|
[this.getColumnKey(column)]: nextFilters,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
// Remove filters not in current columns
|
2016-12-03 15:13:10 +08:00
|
|
|
const currentColumnKeys: string[] = [];
|
|
|
|
treeMap(this.columns, c => {
|
|
|
|
if (!c.children) {
|
|
|
|
currentColumnKeys.push(this.getColumnKey(c));
|
|
|
|
}
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
Object.keys(filters).forEach((columnKey) => {
|
|
|
|
if (currentColumnKeys.indexOf(columnKey) < 0) {
|
|
|
|
delete filters[columnKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (props.pagination) {
|
|
|
|
// Reset current prop
|
|
|
|
pagination.current = 1;
|
|
|
|
pagination.onChange(pagination.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newState = {
|
|
|
|
pagination,
|
2016-09-09 13:55:41 +08:00
|
|
|
filters: {},
|
2016-06-19 10:56:42 +08:00
|
|
|
};
|
2017-07-03 16:57:11 +08:00
|
|
|
const filtersToSetState = { ...filters };
|
2016-06-19 10:56:42 +08:00
|
|
|
// Remove filters which is controlled
|
|
|
|
this.getFilteredValueColumns().forEach(col => {
|
|
|
|
const columnKey = this.getColumnKey(col);
|
|
|
|
if (columnKey) {
|
|
|
|
delete filtersToSetState[columnKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (Object.keys(filtersToSetState).length > 0) {
|
|
|
|
newState.filters = filtersToSetState;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Controlled current prop will not respond user interaction
|
2017-01-09 20:49:00 +08:00
|
|
|
if (typeof props.pagination === 'object' && 'current' in (props.pagination as Object)) {
|
2017-07-03 16:57:11 +08:00
|
|
|
newState.pagination = {
|
|
|
|
...pagination,
|
2016-06-19 10:56:42 +08:00
|
|
|
current: this.state.pagination.current,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState(newState, () => {
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
2016-10-24 16:30:38 +08:00
|
|
|
selectionDirty: false,
|
|
|
|
filters,
|
|
|
|
pagination,
|
2017-07-03 16:57:11 +08:00
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelect = (record, rowIndex, e) => {
|
|
|
|
const checked = e.target.checked;
|
2016-11-11 15:26:51 +08:00
|
|
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
|
|
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2016-06-19 10:56:42 +08:00
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
|
|
|
if (checked) {
|
|
|
|
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
|
|
|
} else {
|
|
|
|
selectedRowKeys = selectedRowKeys.filter((i) => key !== i);
|
|
|
|
}
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionDirty: true,
|
|
|
|
});
|
2016-08-03 21:08:13 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelect',
|
|
|
|
record,
|
|
|
|
checked,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleRadioSelect = (record, rowIndex, e) => {
|
|
|
|
const checked = e.target.checked;
|
2016-11-11 15:26:51 +08:00
|
|
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
|
|
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2016-06-19 10:56:42 +08:00
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
|
|
|
selectedRowKeys = [key];
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionDirty: true,
|
|
|
|
});
|
2016-08-03 21:08:13 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelect',
|
|
|
|
record,
|
|
|
|
checked,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-23 19:29:47 +08:00
|
|
|
handleSelectRow = (selectionKey, index, onSelectFunc) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const data = this.getFlatCurrentPageData();
|
2016-11-11 15:26:51 +08:00
|
|
|
const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection();
|
|
|
|
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2017-02-23 19:29:47 +08:00
|
|
|
const changeableRowKeys = data
|
2016-12-16 17:53:08 +08:00
|
|
|
.filter((item, i) => !this.getCheckboxPropsByItem(item, i).disabled)
|
2016-06-19 10:56:42 +08:00
|
|
|
.map((item, i) => this.getRecordKey(item, i));
|
|
|
|
|
2017-02-23 19:29:47 +08:00
|
|
|
let changeRowKeys: string[] = [];
|
|
|
|
let selectWay = '';
|
|
|
|
let checked;
|
|
|
|
// handle default selection
|
|
|
|
switch (selectionKey) {
|
|
|
|
case 'all':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) < 0) {
|
|
|
|
selectedRowKeys.push(key);
|
|
|
|
changeRowKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectWay = 'onSelectAll';
|
|
|
|
checked = true;
|
|
|
|
break;
|
|
|
|
case 'removeAll':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) >= 0) {
|
|
|
|
selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
|
|
|
|
changeRowKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectWay = 'onSelectAll';
|
|
|
|
checked = false;
|
|
|
|
break;
|
|
|
|
case 'invert':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) < 0) {
|
|
|
|
selectedRowKeys.push(key);
|
2017-10-21 20:06:12 +08:00
|
|
|
} else {
|
2017-02-23 19:29:47 +08:00
|
|
|
selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
changeRowKeys.push(key);
|
2017-02-23 19:29:47 +08:00
|
|
|
selectWay = 'onSelectInvert';
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
2016-06-19 10:56:42 +08:00
|
|
|
selectionDirty: true,
|
|
|
|
});
|
2017-02-23 19:29:47 +08:00
|
|
|
// when select custom selection, callback selections[n].onSelect
|
2017-09-19 09:56:19 +08:00
|
|
|
const { rowSelection } = this.props;
|
|
|
|
let customSelectionStartIndex = 2;
|
|
|
|
if (rowSelection && rowSelection.hideDefaultSelections) {
|
|
|
|
customSelectionStartIndex = 0;
|
|
|
|
}
|
|
|
|
if (index >= customSelectionStartIndex && typeof onSelectFunc === 'function') {
|
2017-02-23 19:29:47 +08:00
|
|
|
return onSelectFunc(changeableRowKeys);
|
|
|
|
}
|
2016-08-03 21:08:13 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
2017-02-23 19:29:47 +08:00
|
|
|
selectWay: selectWay,
|
2016-08-03 21:08:13 +08:00
|
|
|
checked,
|
|
|
|
changeRowKeys,
|
|
|
|
});
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 13:49:22 +08:00
|
|
|
handlePageChange = (current, ...otherArguments) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
const props = this.props;
|
2017-07-03 16:57:11 +08:00
|
|
|
let pagination = { ...this.state.pagination };
|
2016-06-19 10:56:42 +08:00
|
|
|
if (current) {
|
|
|
|
pagination.current = current;
|
|
|
|
} else {
|
|
|
|
pagination.current = pagination.current || 1;
|
|
|
|
}
|
2017-02-19 13:49:22 +08:00
|
|
|
pagination.onChange(pagination.current, ...otherArguments);
|
2016-06-19 10:56:42 +08:00
|
|
|
|
|
|
|
const newState = {
|
|
|
|
pagination,
|
|
|
|
};
|
|
|
|
// Controlled current prop will not respond user interaction
|
2017-03-02 11:30:16 +08:00
|
|
|
if (props.pagination &&
|
|
|
|
typeof props.pagination === 'object' &&
|
|
|
|
'current' in (props.pagination as Object)) {
|
2017-07-03 16:57:11 +08:00
|
|
|
newState.pagination = {
|
|
|
|
...pagination,
|
2016-06-19 10:56:42 +08:00
|
|
|
current: this.state.pagination.current,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
this.setState(newState);
|
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
|
|
|
|
2016-10-24 16:30:38 +08:00
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
2016-10-24 16:30:38 +08:00
|
|
|
selectionDirty: false,
|
|
|
|
pagination,
|
2017-07-03 16:57:11 +08:00
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
renderSelectionBox = (type) => {
|
|
|
|
return (_, record, index) => {
|
|
|
|
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
2016-12-16 17:53:08 +08:00
|
|
|
const props = this.getCheckboxPropsByItem(record, index);
|
2016-11-11 15:26:51 +08:00
|
|
|
const handleChange = (e) => {
|
|
|
|
type === 'radio' ? this.handleRadioSelect(record, rowIndex, e) :
|
|
|
|
this.handleSelect(record, rowIndex, e);
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
return (
|
|
|
|
<span onClick={stopPropagation}>
|
|
|
|
<SelectionBox
|
|
|
|
type={type}
|
|
|
|
store={this.store}
|
|
|
|
rowIndex={rowIndex}
|
|
|
|
disabled={props.disabled}
|
|
|
|
onChange={handleChange}
|
|
|
|
defaultSelection={this.getDefaultSelection()}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 17:53:08 +08:00
|
|
|
getRecordKey = (record, index): string => {
|
2016-08-11 15:38:03 +08:00
|
|
|
const rowKey = this.props.rowKey;
|
2016-12-16 17:53:08 +08:00
|
|
|
const recordKey = (typeof rowKey === 'function') ?
|
|
|
|
rowKey(record, index) : record[rowKey as string];
|
2016-09-27 13:41:57 +08:00
|
|
|
warning(recordKey !== undefined,
|
2017-03-05 18:35:49 +08:00
|
|
|
'Each record in dataSource of table should have a unique `key` prop, or set `rowKey` to an unique primary key,' +
|
2017-07-19 13:59:53 +08:00
|
|
|
'see https://u.ant.design/table-row-key',
|
2016-09-27 13:41:57 +08:00
|
|
|
);
|
2016-12-30 13:50:11 +08:00
|
|
|
return recordKey === undefined ? index : recordKey;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer = () => {
|
2017-05-24 16:16:42 +08:00
|
|
|
return findDOMNode(this) as HTMLElement;
|
2017-05-24 12:17:40 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
renderRowSelection(locale) {
|
2016-10-24 16:30:38 +08:00
|
|
|
const { prefixCls, rowSelection } = this.props;
|
2016-11-22 10:11:12 +08:00
|
|
|
const columns = this.columns.concat();
|
2016-10-24 16:30:38 +08:00
|
|
|
if (rowSelection) {
|
2016-12-16 17:53:08 +08:00
|
|
|
const data = this.getFlatCurrentPageData().filter((item, index) => {
|
2016-10-24 16:30:38 +08:00
|
|
|
if (rowSelection.getCheckboxProps) {
|
2016-12-16 17:53:08 +08:00
|
|
|
return !this.getCheckboxPropsByItem(item, index).disabled;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2017-03-24 11:44:36 +08:00
|
|
|
let selectionColumnClass = classNames(`${prefixCls}-selection-column`, {
|
|
|
|
[`${prefixCls}-selection-column-custom`]: rowSelection.selections,
|
|
|
|
});
|
2016-11-22 10:11:12 +08:00
|
|
|
const selectionColumn: ColumnProps<any> = {
|
2016-11-11 15:26:51 +08:00
|
|
|
key: 'selection-column',
|
|
|
|
render: this.renderSelectionBox(rowSelection.type),
|
2017-03-24 11:44:36 +08:00
|
|
|
className: selectionColumnClass,
|
2016-11-11 15:26:51 +08:00
|
|
|
};
|
|
|
|
if (rowSelection.type !== 'radio') {
|
2016-12-16 17:53:08 +08:00
|
|
|
const checkboxAllDisabled = data.every((item, index) => this.getCheckboxPropsByItem(item, index).disabled);
|
2016-11-11 15:26:51 +08:00
|
|
|
selectionColumn.title = (
|
|
|
|
<SelectionCheckboxAll
|
|
|
|
store={this.store}
|
2017-10-20 14:54:38 +08:00
|
|
|
locale={locale}
|
2016-11-11 15:26:51 +08:00
|
|
|
data={data}
|
|
|
|
getCheckboxPropsByItem={this.getCheckboxPropsByItem}
|
|
|
|
getRecordKey={this.getRecordKey}
|
2016-06-19 10:56:42 +08:00
|
|
|
disabled={checkboxAllDisabled}
|
2017-02-23 19:29:47 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
onSelect={this.handleSelectRow}
|
2017-03-24 11:44:36 +08:00
|
|
|
selections={rowSelection.selections}
|
2017-08-27 15:44:22 +08:00
|
|
|
hideDefaultSelections={rowSelection.hideDefaultSelections}
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer={this.getPopupContainer}
|
2016-06-19 10:56:42 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (columns.some(column => column.fixed === 'left' || column.fixed === true)) {
|
|
|
|
selectionColumn.fixed = 'left';
|
|
|
|
}
|
|
|
|
if (columns[0] && columns[0].key === 'selection-column') {
|
|
|
|
columns[0] = selectionColumn;
|
|
|
|
} else {
|
|
|
|
columns.unshift(selectionColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
2016-08-11 15:38:03 +08:00
|
|
|
getColumnKey(column, index?) {
|
2016-06-19 10:56:42 +08:00
|
|
|
return column.key || column.dataIndex || index;
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:45:45 +08:00
|
|
|
getMaxCurrent(total) {
|
|
|
|
const { current, pageSize } = this.state.pagination;
|
|
|
|
if ((current - 1) * pageSize >= total) {
|
2017-03-16 23:01:13 +08:00
|
|
|
return Math.floor((total - 1) / pageSize) + 1;
|
2016-07-12 18:45:45 +08:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
isSortColumn(column) {
|
|
|
|
const { sortColumn } = this.state;
|
|
|
|
if (!column || !sortColumn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
renderColumnsDropdown(columns, locale) {
|
2016-09-14 16:18:33 +08:00
|
|
|
const { prefixCls, dropdownPrefixCls } = this.props;
|
2016-06-19 10:56:42 +08:00
|
|
|
const { sortOrder } = this.state;
|
2016-09-09 13:55:41 +08:00
|
|
|
return treeMap(columns, (originColumn, i) => {
|
2017-07-03 16:57:11 +08:00
|
|
|
let column = { ...originColumn };
|
2016-06-19 10:56:42 +08:00
|
|
|
let key = this.getColumnKey(column, i);
|
|
|
|
let filterDropdown;
|
|
|
|
let sortButton;
|
2016-06-20 16:36:37 +08:00
|
|
|
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
|
2016-06-19 10:56:42 +08:00
|
|
|
let colFilters = this.state.filters[key] || [];
|
|
|
|
filterDropdown = (
|
2016-06-20 16:25:35 +08:00
|
|
|
<FilterDropdown
|
|
|
|
locale={locale}
|
|
|
|
column={column}
|
2016-06-19 10:56:42 +08:00
|
|
|
selectedKeys={colFilters}
|
|
|
|
confirmFilter={this.handleFilter}
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls={`${prefixCls}-filter`}
|
2016-10-24 16:30:38 +08:00
|
|
|
dropdownPrefixCls={dropdownPrefixCls || 'ant-dropdown'}
|
2017-05-24 12:17:40 +08:00
|
|
|
getPopupContainer={this.getPopupContainer}
|
2016-06-19 10:56:42 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (column.sorter) {
|
|
|
|
let isSortColumn = this.isSortColumn(column);
|
|
|
|
if (isSortColumn) {
|
2017-09-01 14:40:29 +08:00
|
|
|
column.className = column.className || '';
|
|
|
|
if (sortOrder) {
|
|
|
|
column.className += ` ${prefixCls}-column-sort`;
|
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
const isAscend = isSortColumn && sortOrder === 'ascend';
|
|
|
|
const isDescend = isSortColumn && sortOrder === 'descend';
|
|
|
|
sortButton = (
|
2016-09-14 16:18:33 +08:00
|
|
|
<div className={`${prefixCls}-column-sorter`}>
|
2016-11-23 17:53:10 +08:00
|
|
|
<span
|
|
|
|
className={`${prefixCls}-column-sorter-up ${isAscend ? 'on' : 'off'}`}
|
2016-06-19 10:56:42 +08:00
|
|
|
title="↑"
|
|
|
|
onClick={() => this.toggleSortOrder('ascend', column)}
|
|
|
|
>
|
|
|
|
<Icon type="caret-up" />
|
|
|
|
</span>
|
2016-11-23 17:53:10 +08:00
|
|
|
<span
|
|
|
|
className={`${prefixCls}-column-sorter-down ${isDescend ? 'on' : 'off'}`}
|
2016-06-19 10:56:42 +08:00
|
|
|
title="↓"
|
|
|
|
onClick={() => this.toggleSortOrder('descend', column)}
|
|
|
|
>
|
|
|
|
<Icon type="caret-down" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
column.title = (
|
|
|
|
<span>
|
|
|
|
{column.title}
|
|
|
|
{sortButton}
|
|
|
|
{filterDropdown}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
return column;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleShowSizeChange = (current, pageSize) => {
|
|
|
|
const pagination = this.state.pagination;
|
|
|
|
pagination.onShowSizeChange(current, pageSize);
|
2017-07-03 16:57:11 +08:00
|
|
|
const nextPagination = {
|
|
|
|
...pagination,
|
|
|
|
pageSize,
|
|
|
|
current,
|
|
|
|
};
|
2016-06-19 10:56:42 +08:00
|
|
|
this.setState({ pagination: nextPagination });
|
2016-10-24 16:30:38 +08:00
|
|
|
|
|
|
|
const onChange = this.props.onChange;
|
|
|
|
if (onChange) {
|
2017-07-03 16:57:11 +08:00
|
|
|
onChange.apply(null, this.prepareParamsArguments({
|
|
|
|
...this.state,
|
2016-10-24 16:30:38 +08:00
|
|
|
pagination: nextPagination,
|
2017-07-03 16:57:11 +08:00
|
|
|
}));
|
2016-10-24 16:30:38 +08:00
|
|
|
}
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderPagination() {
|
|
|
|
// 强制不需要分页
|
|
|
|
if (!this.hasPagination()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let size = 'default';
|
2016-07-12 18:24:16 +08:00
|
|
|
const { pagination } = this.state;
|
|
|
|
if (pagination.size) {
|
|
|
|
size = pagination.size;
|
2016-10-18 11:55:00 +08:00
|
|
|
} else if (this.props.size as string === 'middle' || this.props.size === 'small') {
|
2016-06-19 10:56:42 +08:00
|
|
|
size = 'small';
|
|
|
|
}
|
2016-07-12 18:24:16 +08:00
|
|
|
let total = pagination.total || this.getLocalData().length;
|
2017-03-23 21:15:49 +08:00
|
|
|
return (total > 0) ? (
|
2016-07-12 18:24:16 +08:00
|
|
|
<Pagination
|
2017-02-16 15:36:34 +08:00
|
|
|
key="pagination"
|
2016-07-12 18:24:16 +08:00
|
|
|
{...pagination}
|
2017-06-12 11:35:29 +08:00
|
|
|
className={classNames(pagination.className, `${this.props.prefixCls}-pagination`)}
|
2016-06-19 10:56:42 +08:00
|
|
|
onChange={this.handlePageChange}
|
|
|
|
total={total}
|
|
|
|
size={size}
|
2016-07-12 18:45:45 +08:00
|
|
|
current={this.getMaxCurrent(total)}
|
2016-06-19 10:56:42 +08:00
|
|
|
onShowSizeChange={this.handleShowSizeChange}
|
2017-03-23 21:15:49 +08:00
|
|
|
/>
|
|
|
|
) : null;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 13:55:36 +08:00
|
|
|
// Get pagination, filters, sorter
|
2016-08-11 15:38:03 +08:00
|
|
|
prepareParamsArguments(state: any): [any, string[], Object] {
|
2017-02-19 13:55:36 +08:00
|
|
|
const pagination = { ...state.pagination };
|
|
|
|
// remove useless handle function in Table.onChange
|
|
|
|
delete pagination.onChange;
|
|
|
|
delete pagination.onShowSizeChange;
|
2016-06-19 10:56:42 +08:00
|
|
|
const filters = state.filters;
|
2016-08-11 15:38:03 +08:00
|
|
|
const sorter: any = {};
|
2016-06-19 10:56:42 +08:00
|
|
|
if (state.sortColumn && state.sortOrder) {
|
|
|
|
sorter.column = state.sortColumn;
|
|
|
|
sorter.order = state.sortOrder;
|
|
|
|
sorter.field = state.sortColumn.dataIndex;
|
|
|
|
sorter.columnKey = this.getColumnKey(state.sortColumn);
|
|
|
|
}
|
|
|
|
return [pagination, filters, sorter];
|
|
|
|
}
|
|
|
|
|
|
|
|
findColumn(myKey) {
|
2016-12-03 15:13:10 +08:00
|
|
|
let column;
|
|
|
|
treeMap(this.columns, c => {
|
|
|
|
if (this.getColumnKey(c) === myKey) {
|
|
|
|
column = c;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return column;
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentPageData() {
|
|
|
|
let data = this.getLocalData();
|
|
|
|
let current;
|
|
|
|
let pageSize;
|
|
|
|
let state = this.state;
|
|
|
|
// 如果没有分页的话,默认全部展示
|
|
|
|
if (!this.hasPagination()) {
|
|
|
|
pageSize = Number.MAX_VALUE;
|
|
|
|
current = 1;
|
|
|
|
} else {
|
|
|
|
pageSize = state.pagination.pageSize;
|
2016-07-12 18:45:45 +08:00
|
|
|
current = this.getMaxCurrent(state.pagination.total || data.length);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
2016-07-12 18:45:45 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
// 分页
|
|
|
|
// ---
|
|
|
|
// 当数据量少于等于每页数量时,直接设置数据
|
|
|
|
// 否则进行读取分页数据
|
|
|
|
if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
|
2016-10-20 22:10:46 +08:00
|
|
|
data = data.filter((_, i) => {
|
2016-06-19 10:56:42 +08:00
|
|
|
return i >= (current - 1) * pageSize && i < current * pageSize;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-08-03 21:27:45 +08:00
|
|
|
getFlatData() {
|
|
|
|
return flatArray(this.getLocalData());
|
|
|
|
}
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
getFlatCurrentPageData() {
|
|
|
|
return flatArray(this.getCurrentPageData());
|
|
|
|
}
|
|
|
|
|
2016-08-26 15:04:19 +08:00
|
|
|
recursiveSort(data, sorterFn) {
|
2016-10-24 16:30:38 +08:00
|
|
|
const { childrenColumnName = 'children' } = this.props;
|
2017-07-03 16:57:11 +08:00
|
|
|
return data.sort(sorterFn).map(item => (item[childrenColumnName] ? {
|
|
|
|
...item,
|
2017-10-08 17:58:42 +08:00
|
|
|
[childrenColumnName]: this.recursiveSort(item[childrenColumnName], sorterFn),
|
|
|
|
} : item));
|
2016-08-26 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
getLocalData() {
|
|
|
|
const state = this.state;
|
2016-08-26 15:04:19 +08:00
|
|
|
const { dataSource } = this.props;
|
2016-08-25 21:12:23 +08:00
|
|
|
let data = dataSource || [];
|
2016-06-19 10:56:42 +08:00
|
|
|
// 优化本地排序
|
|
|
|
data = data.slice(0);
|
|
|
|
const sorterFn = this.getSorterFn();
|
|
|
|
if (sorterFn) {
|
2016-08-26 15:04:19 +08:00
|
|
|
data = this.recursiveSort(data, sorterFn);
|
2016-06-19 10:56:42 +08:00
|
|
|
}
|
|
|
|
// 筛选
|
|
|
|
if (state.filters) {
|
|
|
|
Object.keys(state.filters).forEach((columnKey) => {
|
2016-12-03 15:51:44 +08:00
|
|
|
let col = this.findColumn(columnKey) as any;
|
2016-06-19 10:56:42 +08:00
|
|
|
if (!col) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let values = state.filters[columnKey] || [];
|
|
|
|
if (values.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-24 16:30:38 +08:00
|
|
|
const onFilter = col.onFilter;
|
|
|
|
data = onFilter ? data.filter(record => {
|
|
|
|
return values.some(v => onFilter(v, record));
|
2016-06-19 10:56:42 +08:00
|
|
|
}) : data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
renderTable = (contextLocale) => {
|
|
|
|
const locale = { ...contextLocale, ...this.props.locale };
|
2016-12-27 18:24:30 +08:00
|
|
|
const { style, className, prefixCls, showHeader, ...restProps } = this.props;
|
2016-08-30 17:01:51 +08:00
|
|
|
const data = this.getCurrentPageData();
|
2016-06-19 10:56:42 +08:00
|
|
|
const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
|
|
|
|
|
|
|
|
const classString = classNames({
|
2016-09-14 16:18:33 +08:00
|
|
|
[`${prefixCls}-${this.props.size}`]: true,
|
|
|
|
[`${prefixCls}-bordered`]: this.props.bordered,
|
|
|
|
[`${prefixCls}-empty`]: !data.length,
|
2016-12-27 18:24:30 +08:00
|
|
|
[`${prefixCls}-without-column-header`]: !showHeader,
|
2016-06-19 10:56:42 +08:00
|
|
|
});
|
|
|
|
|
2017-10-20 14:54:38 +08:00
|
|
|
let columns = this.renderRowSelection(locale);
|
|
|
|
columns = this.renderColumnsDropdown(columns, locale);
|
2016-06-19 10:56:42 +08:00
|
|
|
columns = columns.map((column, i) => {
|
2017-07-03 16:57:11 +08:00
|
|
|
const newColumn = { ...column };
|
2016-06-19 10:56:42 +08:00
|
|
|
newColumn.key = this.getColumnKey(newColumn, i);
|
|
|
|
return newColumn;
|
|
|
|
});
|
2016-09-20 13:16:58 +08:00
|
|
|
let expandIconColumnIndex = (columns[0] && columns[0].key === 'selection-column') ? 1 : 0;
|
|
|
|
if ('expandIconColumnIndex' in restProps) {
|
2016-12-19 15:19:15 +08:00
|
|
|
expandIconColumnIndex = restProps.expandIconColumnIndex as number;
|
2016-09-20 13:16:58 +08:00
|
|
|
}
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2016-09-20 13:16:58 +08:00
|
|
|
<RcTable
|
2017-02-16 15:36:34 +08:00
|
|
|
key="table"
|
2016-09-20 13:16:58 +08:00
|
|
|
{...restProps}
|
2016-09-14 16:59:45 +08:00
|
|
|
prefixCls={prefixCls}
|
2016-08-14 15:38:43 +08:00
|
|
|
data={data}
|
|
|
|
columns={columns}
|
2016-12-27 18:24:30 +08:00
|
|
|
showHeader={showHeader}
|
2016-08-14 15:38:43 +08:00
|
|
|
className={classString}
|
2016-09-20 13:16:58 +08:00
|
|
|
expandIconColumnIndex={expandIconColumnIndex}
|
2016-08-14 15:38:43 +08:00
|
|
|
expandIconAsCell={expandIconAsCell}
|
2016-08-30 17:01:51 +08:00
|
|
|
emptyText={() => locale.emptyText}
|
2016-08-14 15:38:43 +08:00
|
|
|
/>
|
2016-06-19 10:56:42 +08:00
|
|
|
);
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { style, className, prefixCls } = this.props;
|
|
|
|
const data = this.getCurrentPageData();
|
|
|
|
|
|
|
|
const table = (
|
|
|
|
<LocaleReceiver
|
|
|
|
componentName="Table"
|
2017-10-26 10:50:18 +08:00
|
|
|
defaultLocale={defaultLocale.Table}
|
2017-10-20 14:54:38 +08:00
|
|
|
>
|
|
|
|
{this.renderTable}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
2017-02-16 15:36:34 +08:00
|
|
|
|
2016-06-19 10:56:42 +08:00
|
|
|
// if there is no pagination or no data,
|
|
|
|
// the height of spin should decrease by half of pagination
|
|
|
|
const paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
|
2017-02-16 15:36:34 +08:00
|
|
|
? `${prefixCls}-with-pagination` : `${prefixCls}-without-pagination`;
|
|
|
|
|
2017-02-13 15:48:08 +08:00
|
|
|
let loading = this.props.loading;
|
2017-02-20 12:18:41 +08:00
|
|
|
if (typeof loading === 'boolean') {
|
2017-02-13 15:48:08 +08:00
|
|
|
loading = {
|
|
|
|
spinning: loading,
|
|
|
|
};
|
|
|
|
}
|
2017-02-20 12:18:41 +08:00
|
|
|
|
2017-02-16 15:36:34 +08:00
|
|
|
return (
|
2017-05-24 12:17:40 +08:00
|
|
|
<div
|
|
|
|
className={classNames(`${prefixCls}-wrapper`, className)}
|
|
|
|
style={style}
|
|
|
|
>
|
2017-02-18 14:58:47 +08:00
|
|
|
<Spin
|
2017-02-20 12:18:41 +08:00
|
|
|
{...loading}
|
2017-02-18 14:58:47 +08:00
|
|
|
className={loading ? `${paginationPatchClass} ${prefixCls}-spin-holder` : ''}
|
|
|
|
>
|
|
|
|
{table}
|
|
|
|
{this.renderPagination()}
|
|
|
|
</Spin>
|
2016-06-19 10:56:42 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|