2019-01-12 11:33:27 +08:00
|
|
|
import VcTable from '../vc-table';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import shallowEqual from 'shallowequal';
|
|
|
|
import FilterDropdown from './filterDropdown';
|
|
|
|
import createStore from './createStore';
|
|
|
|
import SelectionBox from './SelectionBox';
|
|
|
|
import SelectionCheckboxAll from './SelectionCheckboxAll';
|
|
|
|
import Column from './Column';
|
|
|
|
import ColumnGroup from './ColumnGroup';
|
|
|
|
import createBodyRow from './createBodyRow';
|
|
|
|
import { flatArray, treeMap, flatFilter } from './util';
|
2018-03-31 17:46:35 +08:00
|
|
|
import {
|
2019-01-12 11:33:27 +08:00
|
|
|
initDefaultProps,
|
|
|
|
mergeProps,
|
|
|
|
getOptionProps,
|
|
|
|
isValidElement,
|
|
|
|
filterEmpty,
|
|
|
|
getAllProps,
|
2019-09-23 08:38:54 +08:00
|
|
|
getComponentFromProp,
|
2019-01-12 11:33:27 +08:00
|
|
|
} from '../_util/props-util';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2019-04-30 16:59:53 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-01-12 11:33:27 +08:00
|
|
|
import { TableProps } from './interface';
|
2019-08-08 19:02:54 +08:00
|
|
|
import Pagination from '../pagination';
|
|
|
|
import Icon from '../icon';
|
|
|
|
import Spin, { SpinProps } from '../spin';
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
|
|
import defaultLocale from '../locale-provider/default';
|
|
|
|
import warning from '../_util/warning';
|
2019-01-12 11:33:27 +08:00
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
|
|
|
function stopPropagation(e) {
|
|
|
|
e.stopPropagation();
|
2018-04-02 22:13:11 +08:00
|
|
|
if (e.nativeEvent && e.nativeEvent.stopImmediatePropagation) {
|
2019-01-12 11:33:27 +08:00
|
|
|
e.nativeEvent.stopImmediatePropagation();
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getRowSelection(props) {
|
|
|
|
return props.rowSelection || {};
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 17:46:35 +08:00
|
|
|
const defaultPagination = {
|
2018-04-01 16:50:31 +08:00
|
|
|
onChange: noop,
|
|
|
|
onShowSizeChange: noop,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
const ROW_SELECTION_COLUMN_WIDTH = '62px';
|
|
|
|
|
2018-03-31 17:46:35 +08:00
|
|
|
/**
|
|
|
|
* Avoid creating new object, so that parent component's shouldComponentUpdate
|
|
|
|
* can works appropriately。
|
|
|
|
*/
|
2019-01-12 11:33:27 +08:00
|
|
|
const emptyObject = {};
|
2018-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Table',
|
|
|
|
Column,
|
|
|
|
ColumnGroup,
|
2018-03-31 21:11:02 +08:00
|
|
|
mixins: [BaseMixin],
|
2018-03-31 17:46:35 +08:00
|
|
|
props: initDefaultProps(TableProps, {
|
|
|
|
dataSource: [],
|
|
|
|
useFixedHeader: false,
|
2018-03-31 21:11:02 +08:00
|
|
|
// rowSelection: null,
|
2018-09-05 21:28:54 +08:00
|
|
|
size: 'default',
|
2018-03-31 17:46:35 +08:00
|
|
|
loading: false,
|
|
|
|
bordered: false,
|
|
|
|
indentSize: 20,
|
|
|
|
locale: {},
|
|
|
|
rowKey: 'key',
|
|
|
|
showHeader: true,
|
2019-04-30 16:59:53 +08:00
|
|
|
sortDirections: ['ascend', 'descend'],
|
2018-03-31 17:46:35 +08:00
|
|
|
}),
|
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-04-30 16:59:53 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
// CheckboxPropsCache: {
|
|
|
|
// [key: string]: any;
|
|
|
|
// };
|
|
|
|
// store: Store;
|
|
|
|
// columns: ColumnProps<T>[];
|
|
|
|
// components: TableComponents;
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
2018-03-31 17:46:35 +08:00
|
|
|
// this.columns = props.columns || normalizeColumns(props.children)
|
2019-09-23 08:38:54 +08:00
|
|
|
const props = getOptionProps(this);
|
|
|
|
warning(
|
|
|
|
!('expandedRowRender' in props) || !('scroll' in props),
|
|
|
|
'`expandedRowRender` and `scroll` are not compatible. Please use one of them at one time.',
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
this.createComponents(this.components);
|
|
|
|
this.CheckboxPropsCache = {};
|
2018-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
this.store = createStore({
|
2018-09-05 21:28:54 +08:00
|
|
|
selectedRowKeys: getRowSelection(this.$props).selectedRowKeys || [],
|
2018-03-31 17:46:35 +08:00
|
|
|
selectionDirty: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 17:46:35 +08:00
|
|
|
return {
|
|
|
|
...this.getDefaultSortOrder(this.columns),
|
|
|
|
// 减少状态
|
|
|
|
sFilters: this.getFiltersFromColumns(),
|
|
|
|
sPagination: this.getDefaultPagination(this.$props),
|
2018-09-05 21:28:54 +08:00
|
|
|
pivot: undefined,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
|
|
|
watch: {
|
2018-07-17 21:22:48 +08:00
|
|
|
pagination: {
|
2019-01-12 11:33:27 +08:00
|
|
|
handler(val) {
|
2018-07-17 21:22:48 +08:00
|
|
|
this.setState(previousState => {
|
|
|
|
const newPagination = {
|
|
|
|
...defaultPagination,
|
|
|
|
...previousState.sPagination,
|
|
|
|
...val,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
newPagination.current = newPagination.current || 1;
|
|
|
|
newPagination.pageSize = newPagination.pageSize || 10;
|
|
|
|
return { sPagination: val !== false ? newPagination : emptyObject };
|
|
|
|
});
|
2018-07-17 21:22:48 +08:00
|
|
|
},
|
|
|
|
deep: true,
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
|
|
|
rowSelection: {
|
2019-09-23 08:38:54 +08:00
|
|
|
handler(val, oldVal) {
|
2019-01-12 11:33:27 +08:00
|
|
|
if (val && 'selectedRowKeys' in val) {
|
2018-03-31 17:46:35 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectedRowKeys: val.selectedRowKeys || [],
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
const { rowSelection } = this;
|
|
|
|
if (rowSelection && val.getCheckboxProps !== rowSelection.getCheckboxProps) {
|
|
|
|
this.CheckboxPropsCache = {};
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2019-09-23 08:38:54 +08:00
|
|
|
} else if (oldVal && !val) {
|
2019-04-30 16:59:53 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectedRowKeys: [],
|
|
|
|
});
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
deep: true,
|
|
|
|
},
|
2019-02-01 17:23:00 +08:00
|
|
|
dataSource() {
|
2018-03-31 17:46:35 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
this.CheckboxPropsCache = {};
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
columns(val) {
|
2018-03-31 17:46:35 +08:00
|
|
|
if (this.getSortOrderColumns(val).length > 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const sortState = this.getSortStateFromColumns(val);
|
|
|
|
if (
|
|
|
|
sortState.sSortColumn !== this.sSortColumn ||
|
|
|
|
sortState.sSortOrder !== this.sSortOrder
|
|
|
|
) {
|
|
|
|
this.setState(sortState);
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const filteredValueColumns = this.getFilteredValueColumns(val);
|
2018-03-31 17:46:35 +08:00
|
|
|
if (filteredValueColumns.length > 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const filtersFromColumns = this.getFiltersFromColumns(val);
|
|
|
|
const newFilters = { ...this.sFilters };
|
2018-03-31 17:46:35 +08:00
|
|
|
Object.keys(filtersFromColumns).forEach(key => {
|
2019-01-12 11:33:27 +08:00
|
|
|
newFilters[key] = filtersFromColumns[key];
|
|
|
|
});
|
2018-03-31 17:46:35 +08:00
|
|
|
if (this.isFiltersChanged(newFilters)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ sFilters: newFilters });
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
components(val, preVal) {
|
|
|
|
this.createComponents(val, preVal);
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
|
|
|
},
|
2018-03-31 21:11:02 +08:00
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
getCheckboxPropsByItem(item, index) {
|
|
|
|
const rowSelection = getRowSelection(this.$props);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (!rowSelection.getCheckboxProps) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return { props: {} };
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const key = this.getRecordKey(item, index);
|
2018-03-31 21:11:02 +08:00
|
|
|
// Cache checkboxProps
|
|
|
|
if (!this.CheckboxPropsCache[key]) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.CheckboxPropsCache[key] = rowSelection.getCheckboxProps(item);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.CheckboxPropsCache[key].props = this.CheckboxPropsCache[key].props || {};
|
|
|
|
return this.CheckboxPropsCache[key];
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getDefaultSelection() {
|
|
|
|
const rowSelection = getRowSelection(this.$props);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (!rowSelection.getCheckboxProps) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return [];
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
return this.getFlatData()
|
2019-01-12 11:33:27 +08:00
|
|
|
.filter(
|
|
|
|
(item, rowIndex) => this.getCheckboxPropsByItem(item, rowIndex).props.defaultChecked,
|
|
|
|
)
|
|
|
|
.map((record, rowIndex) => this.getRecordKey(record, rowIndex));
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getDefaultPagination(props) {
|
2019-04-30 16:59:53 +08:00
|
|
|
const pagination = typeof props.pagination === 'object' ? props.pagination : {};
|
|
|
|
let current;
|
|
|
|
if ('current' in pagination) {
|
|
|
|
current = pagination.current;
|
|
|
|
} else if ('defaultCurrent' in pagination) {
|
|
|
|
current = pagination.defaultCurrent;
|
|
|
|
}
|
|
|
|
let pageSize;
|
|
|
|
if ('pageSize' in pagination) {
|
|
|
|
pageSize = pagination.pageSize;
|
|
|
|
} else if ('defaultPageSize' in pagination) {
|
|
|
|
pageSize = pagination.defaultPageSize;
|
|
|
|
}
|
2018-03-31 21:11:02 +08:00
|
|
|
return this.hasPagination(props)
|
2018-04-01 16:50:31 +08:00
|
|
|
? {
|
2019-01-12 11:33:27 +08:00
|
|
|
...defaultPagination,
|
|
|
|
...pagination,
|
2019-04-30 16:59:53 +08:00
|
|
|
current: current || 1,
|
|
|
|
pageSize: pageSize || 10,
|
2019-01-12 11:33:27 +08:00
|
|
|
}
|
|
|
|
: {};
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
onRow(prefixCls, record, index) {
|
|
|
|
const { customRow } = this;
|
2019-01-12 11:33:27 +08:00
|
|
|
const custom = customRow ? customRow(record, index) : {};
|
2018-03-31 21:11:02 +08:00
|
|
|
return mergeProps(custom, {
|
|
|
|
props: {
|
|
|
|
prefixCls,
|
|
|
|
store: this.store,
|
|
|
|
rowKey: this.getRecordKey(record, index),
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
setSelectedRowKeys(selectedRowKeys, selectionInfo) {
|
|
|
|
const { selectWay, record, checked, changeRowKeys, nativeEvent } = selectionInfo;
|
|
|
|
const rowSelection = getRowSelection(this.$props);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (rowSelection && !('selectedRowKeys' in rowSelection)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.store.setState({ selectedRowKeys });
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const data = this.getFlatData();
|
2018-04-02 22:13:11 +08:00
|
|
|
if (!rowSelection.onChange && !rowSelection[selectWay]) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
const selectedRows = data.filter(
|
|
|
|
(row, i) => selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-04-02 22:13:11 +08:00
|
|
|
if (rowSelection.onChange) {
|
2019-01-12 11:33:27 +08:00
|
|
|
rowSelection.onChange(selectedRowKeys, selectedRows);
|
2018-04-02 22:13:11 +08:00
|
|
|
}
|
|
|
|
if (selectWay === 'onSelect' && rowSelection.onSelect) {
|
2019-01-12 11:33:27 +08:00
|
|
|
rowSelection.onSelect(record, checked, selectedRows, nativeEvent);
|
2018-09-05 21:28:54 +08:00
|
|
|
} else if (selectWay === 'onSelectMultiple' && rowSelection.onSelectMultiple) {
|
|
|
|
const changeRows = data.filter(
|
|
|
|
(row, i) => changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
rowSelection.onSelectMultiple(checked, selectedRows, changeRows);
|
2018-04-02 22:13:11 +08:00
|
|
|
} else if (selectWay === 'onSelectAll' && rowSelection.onSelectAll) {
|
2018-03-31 21:11:02 +08:00
|
|
|
const changeRows = data.filter(
|
|
|
|
(row, i) => changeRowKeys.indexOf(this.getRecordKey(row, i)) >= 0,
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
rowSelection.onSelectAll(checked, selectedRows, changeRows);
|
2018-04-02 22:13:11 +08:00
|
|
|
} else if (selectWay === 'onSelectInvert' && rowSelection.onSelectInvert) {
|
2019-01-12 11:33:27 +08:00
|
|
|
rowSelection.onSelectInvert(selectedRowKeys);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
hasPagination() {
|
|
|
|
return this.pagination !== false;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
isFiltersChanged(filters) {
|
|
|
|
let filtersChanged = false;
|
2018-03-31 21:11:02 +08:00
|
|
|
if (Object.keys(filters).length !== Object.keys(this.sFilters).length) {
|
2019-01-12 11:33:27 +08:00
|
|
|
filtersChanged = true;
|
2018-03-31 21:11:02 +08:00
|
|
|
} else {
|
|
|
|
Object.keys(filters).forEach(columnKey => {
|
|
|
|
if (filters[columnKey] !== this.sFilters[columnKey]) {
|
2019-01-12 11:33:27 +08:00
|
|
|
filtersChanged = true;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return filtersChanged;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getSortOrderColumns(columns) {
|
|
|
|
return flatFilter(columns || this.columns || [], column => 'sortOrder' in column);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getFilteredValueColumns(columns) {
|
2018-03-31 21:11:02 +08:00
|
|
|
return flatFilter(
|
|
|
|
columns || this.columns || [],
|
2019-01-12 11:33:27 +08:00
|
|
|
column => typeof column.filteredValue !== 'undefined',
|
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getFiltersFromColumns(columns) {
|
|
|
|
const filters = {};
|
|
|
|
this.getFilteredValueColumns(columns).forEach(col => {
|
|
|
|
const colKey = this.getColumnKey(col);
|
|
|
|
filters[colKey] = col.filteredValue;
|
|
|
|
});
|
|
|
|
return filters;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getDefaultSortOrder(columns) {
|
|
|
|
const definedSortState = this.getSortStateFromColumns(columns);
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const defaultSortedColumn = flatFilter(
|
|
|
|
columns || [],
|
|
|
|
column => column.defaultSortOrder != null,
|
|
|
|
)[0];
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
if (defaultSortedColumn && !definedSortState.sortColumn) {
|
|
|
|
return {
|
|
|
|
sSortColumn: defaultSortedColumn,
|
|
|
|
sSortOrder: defaultSortedColumn.defaultSortOrder,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return definedSortState;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getSortStateFromColumns(columns) {
|
2018-03-31 21:11:02 +08:00
|
|
|
// return first column which sortOrder is not falsy
|
2019-01-12 11:33:27 +08:00
|
|
|
const sortedColumn = this.getSortOrderColumns(columns).filter(col => col.sortOrder)[0];
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
if (sortedColumn) {
|
|
|
|
return {
|
|
|
|
sSortColumn: sortedColumn,
|
|
|
|
sSortOrder: sortedColumn.sortOrder,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2018-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
return {
|
2018-03-31 21:11:02 +08:00
|
|
|
sSortColumn: null,
|
|
|
|
sSortOrder: null,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getSorterFn(state) {
|
|
|
|
const { sSortOrder: sortOrder, sSortColumn: sortColumn } = state || this.$data;
|
|
|
|
if (!sortOrder || !sortColumn || typeof sortColumn.sorter !== 'function') {
|
|
|
|
return;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
return (a, b) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const result = sortColumn.sorter(a, b, sortOrder);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (result !== 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return sortOrder === 'descend' ? -result : result;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return 0;
|
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
isSameColumn(a, b) {
|
2018-12-11 20:59:23 +08:00
|
|
|
if (a && b && a.key && a.key === b.key) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return true;
|
2018-12-11 20:59:23 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return (
|
|
|
|
a === b ||
|
|
|
|
shallowEqual(a, b, (value, other) => {
|
|
|
|
if (typeof value === 'function' && typeof other === 'function') {
|
|
|
|
return value === other || value.toString() === other.toString();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2018-12-11 20:59:23 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
toggleSortOrder(column) {
|
2018-12-11 20:59:23 +08:00
|
|
|
if (!column.sorter) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-12-11 20:59:23 +08:00
|
|
|
}
|
2019-04-30 16:59:53 +08:00
|
|
|
const sortDirections = column.sortDirections || this.sortDirections;
|
2019-01-12 11:33:27 +08:00
|
|
|
const { sSortOrder: sortOrder, sSortColumn: sortColumn } = this;
|
2018-03-31 21:11:02 +08:00
|
|
|
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
2019-01-12 11:33:27 +08:00
|
|
|
let newSortOrder;
|
2018-12-11 20:59:23 +08:00
|
|
|
// 切换另一列时,丢弃 sortOrder 的状态
|
2019-04-30 16:59:53 +08:00
|
|
|
if (this.isSameColumn(sortColumn, column) && sortOrder !== undefined) {
|
|
|
|
// 按照sortDirections的内容依次切换排序状态
|
|
|
|
const methodIndex = sortDirections.indexOf(sortOrder) + 1;
|
|
|
|
newSortOrder =
|
2019-05-28 11:37:38 +08:00
|
|
|
methodIndex === sortDirections.length ? undefined : sortDirections[methodIndex];
|
2018-12-11 20:59:23 +08:00
|
|
|
} else {
|
2019-04-30 16:59:53 +08:00
|
|
|
newSortOrder = sortDirections[0];
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
const newState = {
|
2018-12-11 20:59:23 +08:00
|
|
|
sSortOrder: newSortOrder,
|
|
|
|
sSortColumn: newSortOrder ? column : null,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
// Controlled
|
|
|
|
if (this.getSortOrderColumns().length === 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState(newState);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit(
|
|
|
|
'change',
|
|
|
|
...this.prepareParamsArguments({
|
|
|
|
...this.$data,
|
|
|
|
...newState,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleFilter(column, nextFilters) {
|
|
|
|
const props = this.$props;
|
|
|
|
const pagination = { ...this.sPagination };
|
2018-03-31 21:11:02 +08:00
|
|
|
const filters = {
|
|
|
|
...this.sFilters,
|
|
|
|
[this.getColumnKey(column)]: nextFilters,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
// Remove filters not in current columns
|
2019-01-12 11:33:27 +08:00
|
|
|
const currentColumnKeys = [];
|
2018-03-31 21:11:02 +08:00
|
|
|
treeMap(this.columns, c => {
|
|
|
|
if (!c.children) {
|
2019-01-12 11:33:27 +08:00
|
|
|
currentColumnKeys.push(this.getColumnKey(c));
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
Object.keys(filters).forEach(columnKey => {
|
2018-03-31 21:11:02 +08:00
|
|
|
if (currentColumnKeys.indexOf(columnKey) < 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
delete filters[columnKey];
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
if (props.pagination) {
|
|
|
|
// Reset current prop
|
2019-01-12 11:33:27 +08:00
|
|
|
pagination.current = 1;
|
|
|
|
pagination.onChange(pagination.current);
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
const newState = {
|
|
|
|
sPagination: pagination,
|
|
|
|
sFilters: {},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
const filtersToSetState = { ...filters };
|
2018-03-31 21:11:02 +08:00
|
|
|
// Remove filters which is controlled
|
2019-01-12 11:33:27 +08:00
|
|
|
this.getFilteredValueColumns().forEach(col => {
|
|
|
|
const columnKey = this.getColumnKey(col);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (columnKey) {
|
2019-01-12 11:33:27 +08:00
|
|
|
delete filtersToSetState[columnKey];
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
if (Object.keys(filtersToSetState).length > 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
newState.sFilters = filtersToSetState;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
// Controlled current prop will not respond user interaction
|
2019-01-12 11:33:27 +08:00
|
|
|
if (typeof props.pagination === 'object' && 'current' in props.pagination) {
|
2018-04-01 16:50:31 +08:00
|
|
|
newState.sPagination = {
|
|
|
|
...pagination,
|
|
|
|
current: this.sPagination.current,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2018-03-31 21:11:02 +08:00
|
|
|
|
|
|
|
this.setState(newState, () => {
|
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
this.$emit(
|
|
|
|
'change',
|
|
|
|
...this.prepareParamsArguments({
|
|
|
|
...this.$data,
|
|
|
|
sSelectionDirty: false,
|
|
|
|
sFilters: filters,
|
|
|
|
sPagination: pagination,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleSelect(record, rowIndex, e) {
|
|
|
|
const checked = e.target.checked;
|
|
|
|
const nativeEvent = e.nativeEvent;
|
|
|
|
const defaultSelection = this.store.getState().selectionDirty
|
|
|
|
? []
|
|
|
|
: this.getDefaultSelection();
|
|
|
|
let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
|
|
|
const key = this.getRecordKey(record, rowIndex);
|
|
|
|
const { pivot } = this.$data;
|
|
|
|
const rows = this.getFlatCurrentPageData(this.$props.childrenColumnName);
|
|
|
|
let realIndex = rowIndex;
|
2018-09-05 21:28:54 +08:00
|
|
|
if (this.$props.expandedRowRender) {
|
2019-01-12 11:33:27 +08:00
|
|
|
realIndex = rows.findIndex(row => this.getRecordKey(row, rowIndex) === key);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
if (nativeEvent.shiftKey && pivot !== undefined && realIndex !== pivot) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const changeRowKeys = [];
|
|
|
|
const direction = Math.sign(pivot - realIndex);
|
|
|
|
const dist = Math.abs(pivot - realIndex);
|
|
|
|
let step = 0;
|
2018-09-05 21:28:54 +08:00
|
|
|
while (step <= dist) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const i = realIndex + step * direction;
|
|
|
|
step += 1;
|
|
|
|
const row = rows[i];
|
|
|
|
const rowKey = this.getRecordKey(row, i);
|
|
|
|
const checkboxProps = this.getCheckboxPropsByItem(row, i);
|
2018-09-05 21:28:54 +08:00
|
|
|
if (!checkboxProps.disabled) {
|
|
|
|
if (selectedRowKeys.includes(rowKey)) {
|
|
|
|
if (!checked) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys = selectedRowKeys.filter(j => rowKey !== j);
|
|
|
|
changeRowKeys.push(rowKey);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
} else if (checked) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys.push(rowKey);
|
|
|
|
changeRowKeys.push(rowKey);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ pivot: realIndex });
|
2018-09-05 21:28:54 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelectMultiple',
|
|
|
|
record,
|
|
|
|
checked,
|
|
|
|
changeRowKeys,
|
|
|
|
nativeEvent,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
} else {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (checked) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys.push(this.getRecordKey(record, realIndex));
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys = selectedRowKeys.filter(i => key !== i);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ pivot: realIndex });
|
2018-09-05 21:28:54 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelect',
|
|
|
|
record,
|
|
|
|
checked,
|
2019-01-12 11:33:27 +08:00
|
|
|
changeRowKeys: void 0,
|
2018-09-05 21:28:54 +08:00
|
|
|
nativeEvent,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleRadioSelect(record, rowIndex, e) {
|
|
|
|
const checked = e.target.checked;
|
|
|
|
const nativeEvent = e.nativeEvent;
|
|
|
|
const key = this.getRecordKey(record, rowIndex);
|
|
|
|
const selectedRowKeys = [key];
|
2018-03-31 21:11:02 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
|
|
|
selectWay: 'onSelect',
|
|
|
|
record,
|
|
|
|
checked,
|
2019-01-12 11:33:27 +08:00
|
|
|
changeRowKeys: void 0,
|
2018-04-07 14:29:59 +08:00
|
|
|
nativeEvent,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleSelectRow(selectionKey, index, onSelectFunc) {
|
|
|
|
const data = this.getFlatCurrentPageData(this.$props.childrenColumnName);
|
|
|
|
const defaultSelection = this.store.getState().selectionDirty
|
|
|
|
? []
|
|
|
|
: this.getDefaultSelection();
|
|
|
|
const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection);
|
2018-03-31 21:11:02 +08:00
|
|
|
const changeableRowKeys = data
|
2018-06-08 22:30:25 +08:00
|
|
|
.filter((item, i) => !this.getCheckboxPropsByItem(item, i).props.disabled)
|
2019-01-12 11:33:27 +08:00
|
|
|
.map((item, i) => this.getRecordKey(item, i));
|
2018-03-31 21:11:02 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const changeRowKeys = [];
|
|
|
|
let selectWay = 'onSelectAll';
|
|
|
|
let checked;
|
2018-03-31 21:11:02 +08:00
|
|
|
// handle default selection
|
|
|
|
switch (selectionKey) {
|
|
|
|
case 'all':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) < 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys.push(key);
|
|
|
|
changeRowKeys.push(key);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
selectWay = 'onSelectAll';
|
|
|
|
checked = true;
|
|
|
|
break;
|
2018-03-31 21:11:02 +08:00
|
|
|
case 'removeAll':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) >= 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
|
|
|
|
changeRowKeys.push(key);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
selectWay = 'onSelectAll';
|
|
|
|
checked = false;
|
|
|
|
break;
|
2018-03-31 21:11:02 +08:00
|
|
|
case 'invert':
|
|
|
|
changeableRowKeys.forEach(key => {
|
|
|
|
if (selectedRowKeys.indexOf(key) < 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys.push(key);
|
2018-03-31 21:11:02 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectedRowKeys.splice(selectedRowKeys.indexOf(key), 1);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
changeRowKeys.push(key);
|
|
|
|
selectWay = 'onSelectInvert';
|
|
|
|
});
|
|
|
|
break;
|
2018-03-31 21:11:02 +08:00
|
|
|
default:
|
2019-01-12 11:33:27 +08:00
|
|
|
break;
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
// when select custom selection, callback selections[n].onSelect
|
2019-01-12 11:33:27 +08:00
|
|
|
const { rowSelection } = this;
|
|
|
|
let customSelectionStartIndex = 2;
|
2018-03-31 21:11:02 +08:00
|
|
|
if (rowSelection && rowSelection.hideDefaultSelections) {
|
2019-01-12 11:33:27 +08:00
|
|
|
customSelectionStartIndex = 0;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
if (index >= customSelectionStartIndex && typeof onSelectFunc === 'function') {
|
2019-01-12 11:33:27 +08:00
|
|
|
return onSelectFunc(changeableRowKeys);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
this.setSelectedRowKeys(selectedRowKeys, {
|
2018-09-05 21:28:54 +08:00
|
|
|
selectWay,
|
2018-03-31 21:11:02 +08:00
|
|
|
checked,
|
|
|
|
changeRowKeys,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handlePageChange(current, ...otherArguments) {
|
|
|
|
const props = this.$props;
|
|
|
|
const pagination = { ...this.sPagination };
|
2018-03-31 21:11:02 +08:00
|
|
|
if (current) {
|
2019-01-12 11:33:27 +08:00
|
|
|
pagination.current = current;
|
2018-03-31 21:11:02 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
pagination.current = pagination.current || 1;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
pagination.onChange(pagination.current, ...otherArguments);
|
2018-03-31 21:11:02 +08:00
|
|
|
|
|
|
|
const newState = {
|
|
|
|
sPagination: pagination,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
// Controlled current prop will not respond user interaction
|
2019-01-12 11:33:27 +08:00
|
|
|
if (
|
|
|
|
props.pagination &&
|
|
|
|
typeof props.pagination === 'object' &&
|
|
|
|
'current' in props.pagination
|
|
|
|
) {
|
2018-04-01 16:50:31 +08:00
|
|
|
newState.sPagination = {
|
|
|
|
...pagination,
|
|
|
|
current: this.sPagination.current,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState(newState);
|
2018-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
this.store.setState({
|
|
|
|
selectionDirty: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
this.$emit(
|
|
|
|
'change',
|
|
|
|
...this.prepareParamsArguments({
|
|
|
|
...this.$data,
|
|
|
|
sSelectionDirty: false,
|
|
|
|
sPagination: pagination,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
renderSelectionBox(type) {
|
2018-03-31 21:11:02 +08:00
|
|
|
return (_, record, index) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const rowKey = this.getRecordKey(record, index); // 从 1 开始
|
|
|
|
const props = this.getCheckboxPropsByItem(record, index);
|
|
|
|
const handleChange = e => {
|
|
|
|
type === 'radio'
|
|
|
|
? this.handleRadioSelect(record, index, e)
|
|
|
|
: this.handleSelect(record, index, e);
|
|
|
|
};
|
|
|
|
const selectionBoxProps = mergeProps(
|
|
|
|
{
|
|
|
|
props: {
|
|
|
|
type,
|
|
|
|
store: this.store,
|
|
|
|
rowIndex: rowKey,
|
|
|
|
defaultSelection: this.getDefaultSelection(),
|
|
|
|
},
|
|
|
|
on: {
|
|
|
|
change: handleChange,
|
|
|
|
},
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
props,
|
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span onClick={stopPropagation}>
|
2019-01-12 11:33:27 +08:00
|
|
|
<SelectionBox {...selectionBoxProps} />
|
2018-03-31 21:11:02 +08:00
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getRecordKey(record, index) {
|
|
|
|
const { rowKey } = this;
|
|
|
|
const recordKey = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
|
|
|
|
warning(
|
|
|
|
recordKey !== undefined,
|
2018-09-05 21:28:54 +08:00
|
|
|
'Each record in dataSource of table should have a unique `key` prop, or set `rowKey` of Table to an unique primary key,',
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
return recordKey === undefined ? index : recordKey;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getPopupContainer() {
|
|
|
|
return this.$el;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
generatePopupContainerFunc() {
|
|
|
|
const { scroll } = this.$props;
|
|
|
|
|
|
|
|
// Use undefined to let rc component use default logic.
|
|
|
|
return scroll ? this.getPopupContainer : undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderRowSelection(prefixCls, locale) {
|
|
|
|
const { rowSelection, childrenColumnName } = this;
|
2019-01-12 11:33:27 +08:00
|
|
|
const columns = this.columns.concat();
|
2018-03-31 21:11:02 +08:00
|
|
|
if (rowSelection) {
|
2019-01-05 18:09:27 +08:00
|
|
|
const data = this.getFlatCurrentPageData(childrenColumnName).filter((item, index) => {
|
2018-03-31 21:11:02 +08:00
|
|
|
if (rowSelection.getCheckboxProps) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return !this.getCheckboxPropsByItem(item, index).props.disabled;
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return true;
|
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
const selectionColumnClass = classNames(`${prefixCls}-selection-column`, {
|
|
|
|
[`${prefixCls}-selection-column-custom`]: rowSelection.selections,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
const selectionColumn = {
|
|
|
|
key: 'selection-column',
|
2018-04-02 22:13:11 +08:00
|
|
|
customRender: this.renderSelectionBox(rowSelection.type),
|
2018-03-31 21:11:02 +08:00
|
|
|
className: selectionColumnClass,
|
|
|
|
fixed: rowSelection.fixed,
|
2019-04-30 16:59:53 +08:00
|
|
|
width: rowSelection.columnWidth || ROW_SELECTION_COLUMN_WIDTH,
|
2018-09-05 21:28:54 +08:00
|
|
|
title: rowSelection.columnTitle,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
if (rowSelection.type !== 'radio') {
|
2019-01-12 11:33:27 +08:00
|
|
|
const checkboxAllDisabled = data.every(
|
|
|
|
(item, index) => this.getCheckboxPropsByItem(item, index).props.disabled,
|
|
|
|
);
|
2018-09-05 21:28:54 +08:00
|
|
|
selectionColumn.title = selectionColumn.title || (
|
2018-03-31 21:11:02 +08:00
|
|
|
<SelectionCheckboxAll
|
|
|
|
store={this.store}
|
|
|
|
locale={locale}
|
|
|
|
data={data}
|
|
|
|
getCheckboxPropsByItem={this.getCheckboxPropsByItem}
|
|
|
|
getRecordKey={this.getRecordKey}
|
|
|
|
disabled={checkboxAllDisabled}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
onSelect={this.handleSelectRow}
|
|
|
|
selections={rowSelection.selections}
|
|
|
|
hideDefaultSelections={rowSelection.hideDefaultSelections}
|
2019-04-30 16:59:53 +08:00
|
|
|
getPopupContainer={this.generatePopupContainerFunc()}
|
2018-03-31 21:11:02 +08:00
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
if ('fixed' in rowSelection) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectionColumn.fixed = rowSelection.fixed;
|
2018-03-31 21:11:02 +08:00
|
|
|
} else if (columns.some(column => column.fixed === 'left' || column.fixed === true)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
selectionColumn.fixed = 'left';
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
if (columns[0] && columns[0].key === 'selection-column') {
|
2019-01-12 11:33:27 +08:00
|
|
|
columns[0] = selectionColumn;
|
2018-03-31 21:11:02 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
columns.unshift(selectionColumn);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return columns;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getColumnKey(column, index) {
|
|
|
|
return column.key || column.dataIndex || index;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getMaxCurrent(total) {
|
|
|
|
const { current, pageSize } = this.sPagination;
|
2018-03-31 21:11:02 +08:00
|
|
|
if ((current - 1) * pageSize >= total) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return Math.floor((total - 1) / pageSize) + 1;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return current;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
isSortColumn(column) {
|
|
|
|
const { sSortColumn: sortColumn } = this;
|
2018-03-31 21:11:02 +08:00
|
|
|
if (!column || !sortColumn) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return false;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
renderColumnsDropdown(prefixCls, dropdownPrefixCls, columns, locale) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { sSortOrder: sortOrder, sFilters: filters } = this;
|
2018-12-11 20:59:23 +08:00
|
|
|
return treeMap(columns, (column, i) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const key = this.getColumnKey(column, i);
|
|
|
|
let filterDropdown;
|
|
|
|
let sortButton;
|
|
|
|
let customHeaderCell = column.customHeaderCell;
|
2019-08-07 21:56:30 +08:00
|
|
|
const title = this.renderColumnTitle(column.title);
|
2019-01-12 11:33:27 +08:00
|
|
|
const isSortColumn = this.isSortColumn(column);
|
2018-03-31 21:11:02 +08:00
|
|
|
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const colFilters = key in filters ? filters[key] : [];
|
2018-03-31 21:11:02 +08:00
|
|
|
filterDropdown = (
|
|
|
|
<FilterDropdown
|
2018-09-05 21:28:54 +08:00
|
|
|
_propsSymbol={Symbol()}
|
2018-03-31 21:11:02 +08:00
|
|
|
locale={locale}
|
|
|
|
column={column}
|
|
|
|
selectedKeys={colFilters}
|
|
|
|
confirmFilter={this.handleFilter}
|
|
|
|
prefixCls={`${prefixCls}-filter`}
|
|
|
|
dropdownPrefixCls={dropdownPrefixCls || 'ant-dropdown'}
|
2019-04-30 16:59:53 +08:00
|
|
|
getPopupContainer={this.generatePopupContainerFunc()}
|
2019-01-12 11:33:27 +08:00
|
|
|
key="filter-dropdown"
|
2018-03-31 21:11:02 +08:00
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
|
|
|
if (column.sorter) {
|
2019-04-30 16:59:53 +08:00
|
|
|
const sortDirections = column.sortDirections || this.sortDirections;
|
2019-01-12 11:33:27 +08:00
|
|
|
const isAscend = isSortColumn && sortOrder === 'ascend';
|
|
|
|
const isDescend = isSortColumn && sortOrder === 'descend';
|
2019-04-30 16:59:53 +08:00
|
|
|
const ascend = sortDirections.indexOf('ascend') !== -1 && (
|
|
|
|
<Icon
|
|
|
|
class={`${prefixCls}-column-sorter-up ${isAscend ? 'on' : 'off'}`}
|
|
|
|
type="caret-up"
|
|
|
|
theme="filled"
|
2019-09-23 08:38:54 +08:00
|
|
|
key="caret-up"
|
2019-04-30 16:59:53 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const descend = sortDirections.indexOf('descend') !== -1 && (
|
|
|
|
<Icon
|
|
|
|
class={`${prefixCls}-column-sorter-down ${isDescend ? 'on' : 'off'}`}
|
|
|
|
type="caret-down"
|
|
|
|
theme="filled"
|
2019-09-23 08:38:54 +08:00
|
|
|
key="caret-down"
|
2019-04-30 16:59:53 +08:00
|
|
|
/>
|
|
|
|
);
|
2019-08-08 19:04:21 +08:00
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
sortButton = (
|
2019-04-30 16:59:53 +08:00
|
|
|
<div title={locale.sortTitle} class={`${prefixCls}-column-sorter`} key="sorter">
|
|
|
|
{ascend}
|
|
|
|
{descend}
|
2018-03-31 21:11:02 +08:00
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
|
|
|
customHeaderCell = col => {
|
|
|
|
let colProps = {};
|
2019-01-05 18:09:27 +08:00
|
|
|
// Get original first
|
|
|
|
if (column.customHeaderCell) {
|
|
|
|
colProps = {
|
|
|
|
...column.customHeaderCell(col),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2019-01-05 18:09:27 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
colProps.on = colProps.on || {};
|
2019-01-05 18:09:27 +08:00
|
|
|
// Add sorter logic
|
2019-01-12 11:33:27 +08:00
|
|
|
const onHeaderCellClick = colProps.on.click;
|
2019-01-05 18:09:27 +08:00
|
|
|
colProps.on.click = (...args) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.toggleSortOrder(column);
|
2019-01-05 18:09:27 +08:00
|
|
|
if (onHeaderCellClick) {
|
2019-01-12 11:33:27 +08:00
|
|
|
onHeaderCellClick(...args);
|
2019-01-05 18:09:27 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
return colProps;
|
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2018-12-11 20:59:23 +08:00
|
|
|
return {
|
|
|
|
...column,
|
|
|
|
className: classNames(column.className, {
|
|
|
|
[`${prefixCls}-column-has-actions`]: sortButton || filterDropdown,
|
|
|
|
[`${prefixCls}-column-has-filters`]: filterDropdown,
|
|
|
|
[`${prefixCls}-column-has-sorters`]: sortButton,
|
|
|
|
[`${prefixCls}-column-sort`]: isSortColumn && sortOrder,
|
|
|
|
}),
|
|
|
|
title: [
|
2019-08-25 22:09:55 +08:00
|
|
|
<div key="title" class={sortButton ? `${prefixCls}-column-sorters` : undefined}>
|
2019-08-07 21:56:30 +08:00
|
|
|
{title}
|
2018-12-11 20:59:23 +08:00
|
|
|
{sortButton}
|
|
|
|
</div>,
|
|
|
|
filterDropdown,
|
|
|
|
],
|
2019-01-05 18:09:27 +08:00
|
|
|
customHeaderCell,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
renderColumnTitle(title) {
|
|
|
|
const { sFilters: filters, sSortOrder: sortOrder } = this.$data;
|
2018-12-11 20:59:23 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/11246#issuecomment-405009167
|
|
|
|
if (title instanceof Function) {
|
|
|
|
return title({
|
|
|
|
filters,
|
|
|
|
sortOrder,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-12-11 20:59:23 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return title;
|
2018-12-11 20:59:23 +08:00
|
|
|
},
|
2019-01-05 18:09:27 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleShowSizeChange(current, pageSize) {
|
|
|
|
const pagination = this.sPagination;
|
|
|
|
pagination.onShowSizeChange(current, pageSize);
|
2018-04-01 16:50:31 +08:00
|
|
|
const nextPagination = {
|
|
|
|
...pagination,
|
|
|
|
pageSize,
|
|
|
|
current,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
this.setState({ sPagination: nextPagination });
|
|
|
|
this.$emit(
|
|
|
|
'change',
|
|
|
|
...this.prepareParamsArguments({
|
|
|
|
...this.$data,
|
|
|
|
sPagination: nextPagination,
|
|
|
|
}),
|
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
renderPagination(prefixCls, paginationPosition) {
|
2018-03-31 21:11:02 +08:00
|
|
|
// 强制不需要分页
|
|
|
|
if (!this.hasPagination()) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return null;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
let size = 'default';
|
|
|
|
const { sPagination: pagination } = this;
|
2018-04-01 16:50:31 +08:00
|
|
|
if (pagination.size) {
|
2019-01-12 11:33:27 +08:00
|
|
|
size = pagination.size;
|
2018-03-31 21:11:02 +08:00
|
|
|
} else if (this.size === 'middle' || this.size === 'small') {
|
2019-01-12 11:33:27 +08:00
|
|
|
size = 'small';
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const position = pagination.position || 'bottom';
|
|
|
|
const total = pagination.total || this.getLocalData().length;
|
|
|
|
const { class: cls, style, onChange, onShowSizeChange, ...restProps } = pagination; // eslint-disable-line
|
2018-03-31 21:11:02 +08:00
|
|
|
const paginationProps = mergeProps({
|
2018-04-07 14:29:59 +08:00
|
|
|
key: `pagination-${paginationPosition}`,
|
2019-04-30 16:59:53 +08:00
|
|
|
class: classNames(cls, `${prefixCls}-pagination`),
|
2018-03-31 17:46:35 +08:00
|
|
|
props: {
|
2018-04-01 16:50:31 +08:00
|
|
|
...restProps,
|
2018-03-31 21:11:02 +08:00
|
|
|
total,
|
|
|
|
size,
|
|
|
|
current: this.getMaxCurrent(total),
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
2018-03-31 21:11:02 +08:00
|
|
|
style,
|
2018-03-31 17:46:35 +08:00
|
|
|
on: {
|
2018-03-31 21:11:02 +08:00
|
|
|
change: this.handlePageChange,
|
|
|
|
showSizeChange: this.handleShowSizeChange,
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
return total > 0 && (position === paginationPosition || position === 'both') ? (
|
|
|
|
<Pagination {...paginationProps} />
|
|
|
|
) : null;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get pagination, filters, sorter
|
2019-01-12 11:33:27 +08:00
|
|
|
prepareParamsArguments(state) {
|
|
|
|
const pagination = { ...state.sPagination };
|
2018-03-31 21:11:02 +08:00
|
|
|
// remove useless handle function in Table.onChange
|
2019-01-12 11:33:27 +08:00
|
|
|
delete pagination.onChange;
|
|
|
|
delete pagination.onShowSizeChange;
|
|
|
|
const filters = state.sFilters;
|
|
|
|
const sorter = {};
|
2018-03-31 21:11:02 +08:00
|
|
|
if (state.sSortColumn && state.sSortOrder) {
|
2019-01-12 11:33:27 +08:00
|
|
|
sorter.column = state.sSortColumn;
|
|
|
|
sorter.order = state.sSortOrder;
|
|
|
|
sorter.field = state.sSortColumn.dataIndex;
|
|
|
|
sorter.columnKey = this.getColumnKey(state.sSortColumn);
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2018-12-11 20:59:23 +08:00
|
|
|
const extra = {
|
|
|
|
currentDataSource: this.getLocalData(state),
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-12-11 20:59:23 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return [pagination, filters, sorter, extra];
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
findColumn(myKey) {
|
|
|
|
let column;
|
2018-03-31 21:11:02 +08:00
|
|
|
treeMap(this.columns, c => {
|
|
|
|
if (this.getColumnKey(c) === myKey) {
|
2019-01-12 11:33:27 +08:00
|
|
|
column = c;
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
return column;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getCurrentPageData() {
|
|
|
|
let data = this.getLocalData();
|
|
|
|
let current;
|
|
|
|
let pageSize;
|
|
|
|
const sPagination = this.sPagination;
|
2018-03-31 21:11:02 +08:00
|
|
|
// 如果没有分页的话,默认全部展示
|
|
|
|
if (!this.hasPagination()) {
|
2019-01-12 11:33:27 +08:00
|
|
|
pageSize = Number.MAX_VALUE;
|
|
|
|
current = 1;
|
2018-03-31 21:11:02 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
pageSize = sPagination.pageSize;
|
|
|
|
current = this.getMaxCurrent(sPagination.total || data.length);
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
// 分页
|
|
|
|
// ---
|
|
|
|
// 当数据量少于等于每页数量时,直接设置数据
|
|
|
|
// 否则进行读取分页数据
|
|
|
|
if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
|
|
|
|
data = data.filter((_, i) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
return i >= (current - 1) * pageSize && i < current * pageSize;
|
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return data;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getFlatData() {
|
|
|
|
return flatArray(this.getLocalData(null, false));
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getFlatCurrentPageData(childrenColumnName) {
|
|
|
|
return flatArray(this.getCurrentPageData(), childrenColumnName);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
recursiveSort(data, sorterFn) {
|
|
|
|
const { childrenColumnName = 'children' } = this;
|
|
|
|
return data.sort(sorterFn).map(item =>
|
|
|
|
item[childrenColumnName]
|
|
|
|
? {
|
|
|
|
...item,
|
|
|
|
[childrenColumnName]: this.recursiveSort(item[childrenColumnName], sorterFn),
|
|
|
|
}
|
|
|
|
: item,
|
|
|
|
);
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
getLocalData(state, filter = true) {
|
|
|
|
const currentState = state || this.$data;
|
|
|
|
const { sFilters: filters } = currentState;
|
|
|
|
const { dataSource } = this.$props;
|
|
|
|
let data = dataSource || [];
|
2018-03-31 21:11:02 +08:00
|
|
|
// 优化本地排序
|
2019-01-12 11:33:27 +08:00
|
|
|
data = data.slice(0);
|
|
|
|
const sorterFn = this.getSorterFn(currentState);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (sorterFn) {
|
2019-01-12 11:33:27 +08:00
|
|
|
data = this.recursiveSort(data, sorterFn);
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2018-03-31 21:11:02 +08:00
|
|
|
// 筛选
|
2019-01-05 18:09:27 +08:00
|
|
|
if (filter && filters) {
|
2019-01-12 11:33:27 +08:00
|
|
|
Object.keys(filters).forEach(columnKey => {
|
|
|
|
const col = this.findColumn(columnKey);
|
2018-03-31 21:11:02 +08:00
|
|
|
if (!col) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const values = filters[columnKey] || [];
|
2018-03-31 21:11:02 +08:00
|
|
|
if (values.length === 0) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const onFilter = col.onFilter;
|
|
|
|
data = onFilter
|
|
|
|
? data.filter(record => {
|
|
|
|
return values.some(v => onFilter(v, record));
|
|
|
|
})
|
|
|
|
: data;
|
|
|
|
});
|
2018-03-31 21:11:02 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return data;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
createComponents(components = {}, prevComponents) {
|
|
|
|
const bodyRow = components && components.body && components.body.row;
|
|
|
|
const preBodyRow = prevComponents && prevComponents.body && prevComponents.body.row;
|
2018-09-05 21:28:54 +08:00
|
|
|
if (!this.row || bodyRow !== preBodyRow) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.row = createBodyRow(bodyRow);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
this.customComponents = {
|
|
|
|
...components,
|
|
|
|
body: {
|
2018-03-31 21:11:02 +08:00
|
|
|
...components.body,
|
2018-09-05 21:28:54 +08:00
|
|
|
row: this.row,
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
renderTable(prefixCls, renderEmpty, dropdownPrefixCls, contextLocale, loading) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const locale = { ...contextLocale, ...this.locale };
|
2019-04-30 16:59:53 +08:00
|
|
|
const { showHeader, ...restProps } = getOptionProps(this);
|
2019-01-12 11:33:27 +08:00
|
|
|
const data = this.getCurrentPageData();
|
|
|
|
const expandIconAsCell = this.expandedRowRender && this.expandIconAsCell !== false;
|
2018-03-31 21:11:02 +08:00
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
const mergedLocale = { ...contextLocale, ...locale };
|
|
|
|
if (!locale || !locale.emptyText) {
|
|
|
|
mergedLocale.emptyText = renderEmpty(h, 'Table');
|
|
|
|
}
|
|
|
|
|
2018-03-31 21:11:02 +08:00
|
|
|
const classString = classNames({
|
|
|
|
[`${prefixCls}-${this.size}`]: true,
|
|
|
|
[`${prefixCls}-bordered`]: this.bordered,
|
|
|
|
[`${prefixCls}-empty`]: !data.length,
|
|
|
|
[`${prefixCls}-without-column-header`]: !showHeader,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-04-30 16:59:53 +08:00
|
|
|
let columns = this.renderRowSelection(prefixCls, mergedLocale);
|
|
|
|
columns = this.renderColumnsDropdown(prefixCls, dropdownPrefixCls, columns, mergedLocale);
|
2018-03-31 21:11:02 +08:00
|
|
|
columns = columns.map((column, i) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const newColumn = { ...column };
|
|
|
|
newColumn.key = this.getColumnKey(newColumn, i);
|
|
|
|
return newColumn;
|
|
|
|
});
|
|
|
|
let expandIconColumnIndex = columns[0] && columns[0].key === 'selection-column' ? 1 : 0;
|
2018-03-31 21:11:02 +08:00
|
|
|
if ('expandIconColumnIndex' in restProps) {
|
2019-01-12 11:33:27 +08:00
|
|
|
expandIconColumnIndex = restProps.expandIconColumnIndex;
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2018-03-31 21:11:02 +08:00
|
|
|
const vcTableProps = {
|
|
|
|
key: 'table',
|
|
|
|
props: {
|
|
|
|
...restProps,
|
2019-04-30 16:59:53 +08:00
|
|
|
customRow: (record, index) => this.onRow(prefixCls, record, index),
|
2018-03-31 21:11:02 +08:00
|
|
|
components: this.customComponents,
|
|
|
|
prefixCls,
|
|
|
|
data,
|
|
|
|
columns,
|
|
|
|
showHeader,
|
|
|
|
expandIconColumnIndex,
|
|
|
|
expandIconAsCell,
|
2019-04-30 16:59:53 +08:00
|
|
|
emptyText: !(loading.props && loading.props.spinning) && mergedLocale.emptyText,
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
|
|
|
on: this.$listeners,
|
|
|
|
class: classString,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
return <VcTable {...vcTableProps} />;
|
2018-03-31 21:11:02 +08:00
|
|
|
},
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2019-05-28 11:37:38 +08:00
|
|
|
const { prefixCls: customizePrefixCls, dropdownPrefixCls: customizeDropdownPrefixCls } = this;
|
2019-01-12 11:33:27 +08:00
|
|
|
const data = this.getCurrentPageData();
|
2018-03-31 17:46:35 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
let loading = this.loading;
|
2018-03-31 17:46:35 +08:00
|
|
|
if (typeof loading === 'boolean') {
|
|
|
|
loading = {
|
|
|
|
props: {
|
|
|
|
spinning: loading,
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-05-26 22:47:19 +08:00
|
|
|
} else {
|
|
|
|
loading = {
|
|
|
|
props: { ...loading },
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-31 17:46:35 +08:00
|
|
|
}
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
|
|
|
const renderEmpty = this.configProvider.renderEmpty;
|
2019-04-30 16:59:53 +08:00
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('table', customizePrefixCls);
|
|
|
|
const dropdownPrefixCls = getPrefixCls('dropdown', customizeDropdownPrefixCls);
|
2018-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
const table = (
|
|
|
|
<LocaleReceiver
|
2019-01-12 11:33:27 +08:00
|
|
|
componentName="Table"
|
2018-03-31 17:46:35 +08:00
|
|
|
defaultLocale={defaultLocale.Table}
|
2019-05-28 11:37:38 +08:00
|
|
|
children={locale =>
|
|
|
|
this.renderTable(prefixCls, renderEmpty, dropdownPrefixCls, locale, loading)
|
|
|
|
}
|
2018-03-31 17:46:35 +08:00
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-31 17:46:35 +08:00
|
|
|
|
|
|
|
// if there is no pagination or no data,
|
|
|
|
// the height of spin should decrease by half of pagination
|
2019-01-12 11:33:27 +08:00
|
|
|
const paginationPatchClass =
|
|
|
|
this.hasPagination() && data && data.length !== 0
|
|
|
|
? `${prefixCls}-with-pagination`
|
|
|
|
: `${prefixCls}-without-pagination`;
|
2018-03-31 17:46:35 +08:00
|
|
|
const spinProps = {
|
|
|
|
...loading,
|
2019-01-12 11:33:27 +08:00
|
|
|
class:
|
|
|
|
loading.props && loading.props.spinning
|
|
|
|
? `${paginationPatchClass} ${prefixCls}-spin-holder`
|
|
|
|
: '',
|
|
|
|
};
|
2018-03-31 17:46:35 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<div class={classNames(`${prefixCls}-wrapper`)}>
|
|
|
|
<Spin {...spinProps}>
|
2019-04-30 16:59:53 +08:00
|
|
|
{this.renderPagination(prefixCls, 'top')}
|
2018-03-31 17:46:35 +08:00
|
|
|
{table}
|
2019-04-30 16:59:53 +08:00
|
|
|
{this.renderPagination(prefixCls, 'bottom')}
|
2018-03-31 17:46:35 +08:00
|
|
|
</Spin>
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-31 17:46:35 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|