this.handleSelect(record, rowIndex, e)} />
);
}
getRecordKey(record, index) {
if (this.props.rowKey) {
return this.props.rowKey(record, index);
}
return record.key || index;
}
renderRowSelection() {
let columns = this.props.columns.concat();
if (this.props.rowSelection) {
let data = this.getFlatCurrentPageData().filter((item) => {
if (this.props.rowSelection.getCheckboxProps) {
return !this.props.rowSelection.getCheckboxProps(item).disabled;
}
return true;
});
let checked;
if (!data.length) {
checked = false;
} else {
checked = this.state.selectionDirty
? data.every((item, i) =>
this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
: (
data.every((item, i) =>
this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0) ||
data.every((item) =>
this.props.rowSelection.getCheckboxProps &&
this.props.rowSelection.getCheckboxProps(item).defaultChecked)
);
}
let selectionColumn;
if (this.props.rowSelection.type === 'radio') {
selectionColumn = {
key: 'selection-column',
render: this.renderSelectionRadio,
className: 'ant-table-selection-column',
};
} else {
const checkboxAllDisabled = data.every(item =>
this.props.rowSelection.getCheckboxProps &&
this.props.rowSelection.getCheckboxProps(item).disabled);
const checkboxAll = (
);
selectionColumn = {
key: 'selection-column',
title: checkboxAll,
render: this.renderSelectionCheckBox,
className: 'ant-table-selection-column',
};
}
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;
}
getColumnKey(column, index) {
return column.key || column.dataIndex || index;
}
isSortColumn(column) {
const { sortColumn } = this.state;
if (!column || !sortColumn) {
return false;
}
return this.getColumnKey(sortColumn) === this.getColumnKey(column);
}
renderColumnsDropdown(columns) {
const { sortOrder } = this.state;
const locale = this.getLocale();
return columns.map((originColumn, i) => {
let column = { ...originColumn };
let key = this.getColumnKey(column, i);
let filterDropdown;
let sortButton;
if (column.filters && column.filters.length > 0) {
let colFilters = this.state.filters[key] || [];
filterDropdown = (
);
}
if (column.sorter) {
let isSortColumn = this.isSortColumn(column);
if (isSortColumn) {
column.className = column.className || '';
if (sortOrder) {
column.className += ' ant-table-column-sort';
}
}
const isAscend = isSortColumn && sortOrder === 'ascend';
const isDescend = isSortColumn && sortOrder === 'descend';
sortButton = (
this.toggleSortOrder('ascend', column)}>
this.toggleSortOrder('descend', column)}>
);
}
column.title = (
{column.title}
{sortButton}
{filterDropdown}
);
return column;
});
}
handleShowSizeChange = (current, pageSize) => {
const pagination = this.state.pagination;
pagination.onShowSizeChange(current, pageSize);
const nextPagination = { ...pagination, pageSize, current };
this.setState({ pagination: nextPagination });
this.props.onChange(...this.prepareParamsArguments({
...this.state,
pagination: nextPagination,
}));
}
renderPagination() {
// 强制不需要分页
if (!this.hasPagination()) {
return null;
}
let size = 'default';
if (this.state.pagination.size) {
size = this.state.pagination.size;
} else if (this.props.size === 'middle' || this.props.size === 'small') {
size = 'small';
}
let total = this.state.pagination.total || this.getLocalData().length;
return (total > 0) ?
: null;
}
prepareParamsArguments(state) {
// 准备筛选、排序、分页的参数
const pagination = state.pagination;
const filters = state.filters;
const sorter = {};
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) {
return this.props.columns.filter(c => this.getColumnKey(c) === myKey)[0];
}
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;
current = state.pagination.current;
}
// 分页
// ---
// 当数据量少于等于每页数量时,直接设置数据
// 否则进行读取分页数据
if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
data = data.filter((item, i) => {
return i >= (current - 1) * pageSize && i < current * pageSize;
});
}
return data;
}
getFlatCurrentPageData() {
return flatArray(this.getCurrentPageData());
}
getLocalData() {
const state = this.state;
let data = this.props.dataSource || [];
// 优化本地排序
data = data.slice(0);
for (let i = 0; i < data.length; i++) {
data[i].indexForSort = i;
}
const sorterFn = this.getSorterFn();
if (sorterFn) {
data = data.sort(sorterFn);
}
// 筛选
if (state.filters) {
Object.keys(state.filters).forEach((columnKey) => {
let col = this.findColumn(columnKey);
if (!col) {
return;
}
let values = state.filters[columnKey] || [];
if (values.length === 0) {
return;
}
data = col.onFilter ? data.filter(record => {
return values.some(v => col.onFilter(v, record));
}) : data;
});
}
return data;
}
render() {
const { style, className, ...restProps } = this.props;
const data = this.getCurrentPageData();
let columns = this.renderRowSelection();
const expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
const locale = this.getLocale();
const classString = classNames({
[`ant-table-${this.props.size}`]: true,
'ant-table-bordered': this.props.bordered,
});
columns = this.renderColumnsDropdown(columns);
columns = columns.map((column, i) => {
const newColumn = { ...column };
newColumn.key = this.getColumnKey(newColumn, i);
return newColumn;
});
let emptyText;
let emptyClass = '';
if (!data || data.length === 0) {
emptyText = (
{locale.emptyText}
);
emptyClass = 'ant-table-empty';
}
let table = (
{emptyText}
);
// 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)
? 'ant-table-with-pagination'
: 'ant-table-without-pagination';
const spinClassName = this.props.loading ? `${paginationPatchClass} ant-table-spin-holder` : '';
table = {table};
return (
{table}
{this.renderPagination()}
);
}
}