2015-07-09 11:41:36 +08:00
|
|
|
import React from 'react';
|
2015-10-19 11:34:44 +08:00
|
|
|
import reqwest from 'reqwest';
|
2015-07-09 11:41:36 +08:00
|
|
|
import Table from 'rc-table';
|
2015-07-15 17:22:58 +08:00
|
|
|
import Checkbox from '../checkbox';
|
2015-10-09 17:14:09 +08:00
|
|
|
import Radio from '../radio';
|
2015-08-17 18:14:39 +08:00
|
|
|
import FilterDropdown from './filterDropdown';
|
2015-07-14 15:35:17 +08:00
|
|
|
import Pagination from '../pagination';
|
2015-11-20 11:05:34 +08:00
|
|
|
import Icon from '../icon';
|
2015-07-14 20:04:14 +08:00
|
|
|
import objectAssign from 'object-assign';
|
2015-10-27 10:34:05 +08:00
|
|
|
import Spin from '../spin';
|
2015-07-09 11:41:36 +08:00
|
|
|
|
2015-07-31 10:40:23 +08:00
|
|
|
function noop() {
|
|
|
|
}
|
2015-07-27 19:58:51 +08:00
|
|
|
|
2015-07-31 10:40:23 +08:00
|
|
|
function defaultResolve(data) {
|
|
|
|
return data || [];
|
|
|
|
}
|
2015-07-27 19:58:51 +08:00
|
|
|
|
2015-08-05 01:42:37 +08:00
|
|
|
class DataSource {
|
2015-08-07 01:03:42 +08:00
|
|
|
init(config) {
|
|
|
|
this.config = config;
|
2015-08-05 01:44:21 +08:00
|
|
|
this.url = config.url || '';
|
2015-08-05 01:42:37 +08:00
|
|
|
this.resolve = config.resolve || defaultResolve;
|
|
|
|
this.getParams = config.getParams || noop;
|
|
|
|
this.getPagination = config.getPagination || noop;
|
2015-08-05 10:58:21 +08:00
|
|
|
this.headers = config.headers || {};
|
2015-08-11 16:57:17 +08:00
|
|
|
this.data = config.data || {};
|
2015-08-07 01:03:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(config) {
|
|
|
|
if (config) {
|
|
|
|
this.init(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 21:08:00 +08:00
|
|
|
clone(config = {}) {
|
2015-08-26 14:08:46 +08:00
|
|
|
return new DataSource(objectAssign({}, this.config, config));
|
2015-08-05 01:42:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 16:18:46 +08:00
|
|
|
let AntTable = React.createClass({
|
2015-07-31 10:40:23 +08:00
|
|
|
getInitialState() {
|
2015-07-10 17:47:53 +08:00
|
|
|
return {
|
2015-07-31 10:40:23 +08:00
|
|
|
// 减少状态
|
2015-07-12 17:10:06 +08:00
|
|
|
selectedRowKeys: [],
|
2015-07-31 10:40:23 +08:00
|
|
|
// only for remote
|
|
|
|
data: [],
|
2015-08-27 23:55:25 +08:00
|
|
|
dataSource: this.props.dataSource,
|
2015-07-31 10:40:23 +08:00
|
|
|
filters: {},
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: false,
|
2015-11-03 18:16:16 +08:00
|
|
|
loading: this.props.loading,
|
2015-07-31 10:40:23 +08:00
|
|
|
sortColumn: '',
|
|
|
|
sortOrder: '',
|
|
|
|
sorter: null,
|
2015-10-09 17:14:09 +08:00
|
|
|
radioIndex: null,
|
2015-07-31 10:40:23 +08:00
|
|
|
pagination: this.hasPagination() ? objectAssign({
|
2015-08-27 23:53:42 +08:00
|
|
|
pageSize: 10,
|
|
|
|
current: 1
|
2015-07-31 10:40:23 +08:00
|
|
|
}, this.props.pagination) : {}
|
2015-07-10 17:47:53 +08:00
|
|
|
};
|
|
|
|
},
|
2015-07-31 10:40:23 +08:00
|
|
|
|
2015-07-09 11:41:36 +08:00
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
2015-07-09 14:51:48 +08:00
|
|
|
prefixCls: 'ant-table',
|
2015-07-10 17:47:53 +08:00
|
|
|
useFixedHeader: false,
|
2015-07-10 17:50:48 +08:00
|
|
|
rowSelection: null,
|
2015-10-22 17:27:53 +08:00
|
|
|
className: '',
|
2015-10-22 21:01:52 +08:00
|
|
|
size: 'default',
|
2015-11-03 18:16:16 +08:00
|
|
|
loading: false,
|
2015-10-19 11:29:36 +08:00
|
|
|
bordered: false,
|
2015-10-22 17:27:53 +08:00
|
|
|
onChange: function () {
|
|
|
|
}
|
2015-07-09 11:41:36 +08:00
|
|
|
};
|
|
|
|
},
|
2015-07-31 10:40:23 +08:00
|
|
|
|
2015-08-05 10:58:21 +08:00
|
|
|
propTypes: {
|
2015-08-12 12:51:20 +08:00
|
|
|
dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
|
2015-08-05 10:58:21 +08:00
|
|
|
},
|
|
|
|
|
2015-10-09 17:14:09 +08:00
|
|
|
getDefaultSelection() {
|
|
|
|
let selectedRowKeys = [];
|
|
|
|
if (this.props.rowSelection && this.props.rowSelection.getCheckboxProps) {
|
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
data.filter((item) => {
|
|
|
|
if (this.props.rowSelection.getCheckboxProps) {
|
2015-11-04 17:22:05 +08:00
|
|
|
return this.props.rowSelection.getCheckboxProps(item).defaultChecked;
|
2015-10-09 17:14:09 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}).map((record, rowIndex) => {
|
|
|
|
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return selectedRowKeys;
|
|
|
|
},
|
|
|
|
|
2015-07-31 11:28:49 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2015-07-31 10:40:23 +08:00
|
|
|
if (('pagination' in nextProps) && nextProps.pagination !== false) {
|
2015-09-26 00:55:55 +08:00
|
|
|
this.setState({
|
|
|
|
pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
|
|
|
|
});
|
2015-07-28 14:02:18 +08:00
|
|
|
}
|
2015-08-27 23:55:25 +08:00
|
|
|
// 外界只有 dataSource 的变化会触发新请求
|
|
|
|
if ('dataSource' in nextProps &&
|
2015-11-05 17:30:24 +08:00
|
|
|
nextProps.dataSource !== this.props.dataSource) {
|
|
|
|
let selectedRowKeys = this.state.selectedRowKeys;
|
|
|
|
// 把不在当前页的选中项去掉
|
|
|
|
if (this.isLocalDataSource()) {
|
|
|
|
let currentPageRowKeys = this.getLocalDataPaging(nextProps.dataSource);
|
|
|
|
selectedRowKeys = selectedRowKeys.filter((key) => {
|
|
|
|
return currentPageRowKeys.indexOf(key) >= 0;
|
|
|
|
});
|
|
|
|
}
|
2015-09-26 00:55:55 +08:00
|
|
|
this.setState({
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: false,
|
2015-08-27 23:55:25 +08:00
|
|
|
dataSource: nextProps.dataSource,
|
|
|
|
loading: true
|
2015-09-26 00:55:55 +08:00
|
|
|
}, this.fetch);
|
2015-07-31 10:40:23 +08:00
|
|
|
}
|
|
|
|
if (nextProps.columns !== this.props.columns) {
|
2015-09-26 00:55:55 +08:00
|
|
|
this.setState({
|
|
|
|
filters: {}
|
|
|
|
});
|
2015-07-28 15:20:15 +08:00
|
|
|
}
|
2015-11-03 18:16:16 +08:00
|
|
|
if ('loading' in nextProps) {
|
|
|
|
this.setState({
|
|
|
|
loading: nextProps.loading
|
|
|
|
});
|
|
|
|
}
|
2015-07-28 15:20:15 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
|
|
|
hasPagination(pagination) {
|
2015-07-31 10:40:23 +08:00
|
|
|
if (pagination === undefined) {
|
|
|
|
pagination = this.props.pagination;
|
2015-07-28 15:20:15 +08:00
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
return pagination !== false;
|
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
|
|
|
isLocalDataSource() {
|
2015-08-27 23:55:25 +08:00
|
|
|
return Array.isArray(this.state.dataSource);
|
2015-07-28 14:02:18 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
|
|
|
getRemoteDataSource() {
|
2015-08-27 23:55:25 +08:00
|
|
|
return this.state.dataSource;
|
2015-07-28 14:02:18 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-13 16:22:02 +08:00
|
|
|
toggleSortOrder(order, column) {
|
2015-07-15 11:43:00 +08:00
|
|
|
let sortColumn = this.state.sortColumn;
|
|
|
|
let sortOrder = this.state.sortOrder;
|
2015-07-16 18:26:17 +08:00
|
|
|
let sorter;
|
2015-08-12 12:47:04 +08:00
|
|
|
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
|
|
|
let isSortColumn = this.isSortColumn(column);
|
|
|
|
if (!isSortColumn) { // 当前列未排序
|
|
|
|
sortOrder = order;
|
|
|
|
sortColumn = column;
|
|
|
|
} else { // 当前列已排序
|
|
|
|
if (sortOrder === order) { // 切换为未排序状态
|
|
|
|
sortOrder = '';
|
|
|
|
sortColumn = null;
|
|
|
|
} else { // 切换为排序状态
|
|
|
|
sortOrder = order;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
if (this.isLocalDataSource()) {
|
|
|
|
sorter = function () {
|
2015-07-14 15:35:17 +08:00
|
|
|
let result = column.sorter.apply(this, arguments);
|
2015-07-15 11:43:00 +08:00
|
|
|
if (sortOrder === 'ascend') {
|
2015-07-14 15:35:17 +08:00
|
|
|
return result;
|
2015-07-15 11:43:00 +08:00
|
|
|
} else if (sortOrder === 'descend') {
|
2015-07-14 15:35:17 +08:00
|
|
|
return -result;
|
|
|
|
}
|
|
|
|
};
|
2015-07-13 16:22:02 +08:00
|
|
|
}
|
2015-10-19 11:29:36 +08:00
|
|
|
const newState = {
|
|
|
|
sortOrder,
|
|
|
|
sortColumn,
|
|
|
|
sorter
|
|
|
|
};
|
|
|
|
this.fetch(newState);
|
|
|
|
this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
|
2015-07-13 16:22:02 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-31 10:40:23 +08:00
|
|
|
handleFilter(column, filters) {
|
|
|
|
filters = objectAssign({}, this.state.filters, {
|
|
|
|
[this.getColumnKey(column)]: filters
|
|
|
|
});
|
2015-10-19 11:29:36 +08:00
|
|
|
const newState = {
|
2015-07-31 10:40:23 +08:00
|
|
|
selectedRowKeys: [],
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: false,
|
2015-10-19 11:29:36 +08:00
|
|
|
filters
|
|
|
|
};
|
|
|
|
this.fetch(newState);
|
|
|
|
this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
|
2015-07-13 18:42:08 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-31 10:40:23 +08:00
|
|
|
handleSelect(record, rowIndex, e) {
|
2015-07-16 14:41:28 +08:00
|
|
|
let checked = e.target.checked;
|
2015-10-09 17:14:09 +08:00
|
|
|
let defaultSelection = [];
|
2015-11-05 17:30:24 +08:00
|
|
|
if (!this.state.selectionDirty) {
|
2015-10-09 17:14:09 +08:00
|
|
|
defaultSelection = this.getDefaultSelection();
|
|
|
|
}
|
|
|
|
let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
|
2015-07-31 10:40:23 +08:00
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
2015-07-14 15:35:17 +08:00
|
|
|
if (checked) {
|
2015-07-31 10:40:23 +08:00
|
|
|
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
2015-07-14 15:35:17 +08:00
|
|
|
} else {
|
2015-07-31 10:40:23 +08:00
|
|
|
selectedRowKeys = selectedRowKeys.filter((i) => {
|
|
|
|
return key !== i;
|
2015-07-14 15:35:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.setState({
|
2015-10-09 17:14:09 +08:00
|
|
|
selectedRowKeys: selectedRowKeys,
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: true
|
2015-10-09 17:14:09 +08:00
|
|
|
});
|
|
|
|
if (this.props.rowSelection.onSelect) {
|
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
let selectedRows = data.filter((row, i) => {
|
|
|
|
return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
|
|
|
|
});
|
|
|
|
this.props.rowSelection.onSelect(record, checked, selectedRows);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleRadioSelect: function (record, rowIndex, e) {
|
|
|
|
let checked = e.target.checked;
|
|
|
|
let defaultSelection = [];
|
2015-11-05 17:30:24 +08:00
|
|
|
if (!this.state.selectionDirty) {
|
2015-10-09 17:14:09 +08:00
|
|
|
defaultSelection = this.getDefaultSelection();
|
|
|
|
}
|
|
|
|
let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
|
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
|
|
|
selectedRowKeys = [key];
|
|
|
|
this.setState({
|
|
|
|
selectedRowKeys: selectedRowKeys,
|
|
|
|
radioIndex: record.key,
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: true
|
2015-07-14 15:35:17 +08:00
|
|
|
});
|
|
|
|
if (this.props.rowSelection.onSelect) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
let selectedRows = data.filter((row, i) => {
|
|
|
|
return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
|
2015-07-15 20:22:56 +08:00
|
|
|
});
|
2015-07-31 10:40:23 +08:00
|
|
|
this.props.rowSelection.onSelect(record, checked, selectedRows);
|
2015-07-14 15:35:17 +08:00
|
|
|
}
|
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-16 14:41:28 +08:00
|
|
|
handleSelectAllRow(e) {
|
|
|
|
let checked = e.target.checked;
|
2015-07-31 10:40:23 +08:00
|
|
|
let data = this.getCurrentPageData();
|
2015-10-09 17:14:09 +08:00
|
|
|
let selectedRowKeys = checked ? data.filter((item) => {
|
|
|
|
if (this.props.rowSelection.getCheckboxProps) {
|
|
|
|
return !this.props.rowSelection.getCheckboxProps(item).disabled;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}).map((item, i) => {
|
2015-07-31 10:40:23 +08:00
|
|
|
return this.getRecordKey(item, i);
|
|
|
|
}) : [];
|
2015-07-15 20:22:56 +08:00
|
|
|
this.setState({
|
2015-10-09 17:14:09 +08:00
|
|
|
selectedRowKeys: selectedRowKeys,
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: true
|
2015-07-14 15:35:17 +08:00
|
|
|
});
|
|
|
|
if (this.props.rowSelection.onSelectAll) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let selectedRows = data.filter((row, i) => {
|
|
|
|
return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
|
2015-07-15 20:22:56 +08:00
|
|
|
});
|
|
|
|
this.props.rowSelection.onSelectAll(checked, selectedRows);
|
2015-07-13 18:42:08 +08:00
|
|
|
}
|
2015-07-13 17:40:17 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-28 12:26:54 +08:00
|
|
|
handlePageChange(current) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let pagination = objectAssign({}, this.state.pagination);
|
2015-07-28 12:26:54 +08:00
|
|
|
if (current) {
|
|
|
|
pagination.current = current;
|
|
|
|
} else {
|
|
|
|
pagination.current = pagination.current || 1;
|
|
|
|
}
|
2015-10-19 11:29:36 +08:00
|
|
|
const newState = {
|
2015-07-31 10:40:23 +08:00
|
|
|
// 防止内存泄漏,只维持当页
|
|
|
|
selectedRowKeys: [],
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: false,
|
2015-10-19 11:29:36 +08:00
|
|
|
pagination
|
|
|
|
};
|
|
|
|
this.fetch(newState);
|
|
|
|
this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
|
2015-07-14 15:35:17 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-10-09 17:14:09 +08:00
|
|
|
onRadioChange: function (ev) {
|
|
|
|
this.setState({
|
|
|
|
radioIndex: ev.target.value
|
2015-07-31 10:40:23 +08:00
|
|
|
});
|
2015-07-14 15:35:17 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-10-09 17:14:09 +08:00
|
|
|
renderSelectionRadio(value, record, index) {
|
|
|
|
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
|
|
|
let props = {};
|
|
|
|
if (this.props.rowSelection.getCheckboxProps) {
|
|
|
|
props = this.props.rowSelection.getCheckboxProps.call(this, record);
|
|
|
|
}
|
2015-11-05 17:30:24 +08:00
|
|
|
let checked;
|
|
|
|
if (this.state.selectionDirty) {
|
|
|
|
checked = this.state.radioIndex === record.key;
|
|
|
|
} else {
|
|
|
|
checked = (this.state.radioIndex === record.key ||
|
|
|
|
this.getDefaultSelection().indexOf(rowIndex) >= 0);
|
|
|
|
}
|
2015-10-22 17:27:53 +08:00
|
|
|
return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
|
|
|
|
value={record.key} checked={checked}/>;
|
2015-10-09 17:14:09 +08:00
|
|
|
},
|
|
|
|
|
2015-07-14 15:35:17 +08:00
|
|
|
renderSelectionCheckBox(value, record, index) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
2015-11-05 17:30:24 +08:00
|
|
|
let checked;
|
|
|
|
if (this.state.selectionDirty) {
|
|
|
|
checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
|
|
|
|
} else {
|
|
|
|
checked = (this.state.selectedRowKeys.indexOf(rowIndex) >= 0 ||
|
|
|
|
this.getDefaultSelection().indexOf(rowIndex) >= 0);
|
|
|
|
}
|
2015-10-09 17:14:09 +08:00
|
|
|
let props = {};
|
|
|
|
if (this.props.rowSelection.getCheckboxProps) {
|
|
|
|
props = this.props.rowSelection.getCheckboxProps.call(this, record);
|
|
|
|
}
|
2015-10-22 17:27:53 +08:00
|
|
|
return <Checkbox checked={checked} disabled={props.disabled}
|
|
|
|
onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
|
2015-07-31 10:40:23 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
|
|
|
getRecordKey(record, index) {
|
2015-11-16 15:39:40 +08:00
|
|
|
if (this.props.rowKey) {
|
|
|
|
return this.props.rowKey(record, index);
|
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
return record.key || index;
|
2015-07-14 15:35:17 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-14 15:35:17 +08:00
|
|
|
renderRowSelection() {
|
2015-07-31 10:40:23 +08:00
|
|
|
let columns = this.props.columns.concat();
|
2015-07-14 15:35:17 +08:00
|
|
|
if (this.props.rowSelection) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
let checked;
|
|
|
|
if (!data.length) {
|
|
|
|
checked = false;
|
|
|
|
} else {
|
2015-11-16 15:57:36 +08:00
|
|
|
data = data.filter((item) => {
|
2015-10-09 17:14:09 +08:00
|
|
|
if (this.props.rowSelection.getCheckboxProps) {
|
|
|
|
return !this.props.rowSelection.getCheckboxProps(item).disabled;
|
|
|
|
}
|
|
|
|
return true;
|
2015-07-31 10:40:23 +08:00
|
|
|
});
|
2015-11-16 15:57:36 +08:00
|
|
|
checked = this.state.selectionDirty
|
|
|
|
? data.every((item, i) =>
|
|
|
|
this.state.selectedRowKeys.indexOf(this.getRecordKey(item, i)) >= 0)
|
|
|
|
: data.every((item, i) =>
|
|
|
|
this.props.rowSelection.getCheckboxProps &&
|
|
|
|
this.props.rowSelection.getCheckboxProps(item).defaultChecked);
|
2015-07-31 10:40:23 +08:00
|
|
|
}
|
2015-10-09 17:14:09 +08:00
|
|
|
let selectionColumn;
|
|
|
|
if (this.props.rowSelection.type === 'radio') {
|
|
|
|
selectionColumn = {
|
|
|
|
key: 'selection-column',
|
|
|
|
width: 60,
|
|
|
|
render: this.renderSelectionRadio,
|
|
|
|
className: 'ant-table-selection-column'
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow}/>;
|
|
|
|
selectionColumn = {
|
|
|
|
key: 'selection-column',
|
|
|
|
title: checkboxAll,
|
|
|
|
width: 60,
|
|
|
|
render: this.renderSelectionCheckBox,
|
|
|
|
className: 'ant-table-selection-column'
|
|
|
|
};
|
|
|
|
}
|
2015-07-14 15:35:17 +08:00
|
|
|
if (columns[0] &&
|
2015-07-31 10:40:23 +08:00
|
|
|
columns[0].key === 'selection-column') {
|
2015-07-14 15:35:17 +08:00
|
|
|
columns[0] = selectionColumn;
|
|
|
|
} else {
|
|
|
|
columns.unshift(selectionColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
},
|
2015-07-31 10:40:23 +08:00
|
|
|
|
2015-07-31 11:28:49 +08:00
|
|
|
getCurrentPageData() {
|
2015-07-31 10:40:23 +08:00
|
|
|
return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
|
|
|
|
},
|
|
|
|
|
2015-08-12 12:47:04 +08:00
|
|
|
getColumnKey(column, index) {
|
|
|
|
return column.key || column.dataIndex || index;
|
|
|
|
},
|
|
|
|
|
|
|
|
isSortColumn(column) {
|
|
|
|
if (!column || !this.state.sortColumn) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let colKey = this.getColumnKey(column);
|
|
|
|
let isSortColumn = (this.getColumnKey(this.state.sortColumn) === colKey);
|
|
|
|
return isSortColumn;
|
2015-07-31 10:40:23 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
renderColumnsDropdown(columns) {
|
2015-08-12 11:50:07 +08:00
|
|
|
return columns.map((column, i) => {
|
2015-08-12 11:26:36 +08:00
|
|
|
column = objectAssign({}, column);
|
2015-08-12 12:47:04 +08:00
|
|
|
let key = this.getColumnKey(column, i);
|
2015-08-19 11:41:44 +08:00
|
|
|
let filterDropdown, sortButton;
|
2015-07-14 15:35:17 +08:00
|
|
|
if (column.filters && column.filters.length > 0) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let colFilters = this.state.filters[key] || [];
|
2015-08-17 18:14:39 +08:00
|
|
|
filterDropdown =
|
|
|
|
<FilterDropdown column={column}
|
|
|
|
selectedKeys={colFilters}
|
2015-10-22 17:27:53 +08:00
|
|
|
confirmFilter={this.handleFilter}/>;
|
2015-07-13 16:22:02 +08:00
|
|
|
}
|
|
|
|
if (column.sorter) {
|
2015-08-12 12:47:04 +08:00
|
|
|
let isSortColumn = this.isSortColumn(column);
|
2015-08-12 11:50:07 +08:00
|
|
|
if (isSortColumn) {
|
|
|
|
column.className = column.className || '';
|
2015-08-12 12:47:04 +08:00
|
|
|
if (this.state.sortOrder) {
|
|
|
|
column.className += ' ant-table-column-sort';
|
|
|
|
}
|
2015-08-12 11:50:07 +08:00
|
|
|
}
|
2015-07-13 16:22:02 +08:00
|
|
|
sortButton = <div className="ant-table-column-sorter">
|
|
|
|
<span className={'ant-table-column-sorter-up ' +
|
2015-07-15 11:43:00 +08:00
|
|
|
((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
|
2015-07-31 10:40:23 +08:00
|
|
|
title="升序排序"
|
|
|
|
onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
|
2015-10-22 17:27:53 +08:00
|
|
|
<Icon type="caret-up"/>
|
2015-07-13 16:22:02 +08:00
|
|
|
</span>
|
|
|
|
<span className={'ant-table-column-sorter-down ' +
|
2015-07-15 11:43:00 +08:00
|
|
|
((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
|
2015-07-31 10:40:23 +08:00
|
|
|
title="降序排序"
|
|
|
|
onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
|
2015-10-22 17:27:53 +08:00
|
|
|
<Icon type="caret-down"/>
|
2015-07-13 16:22:02 +08:00
|
|
|
</span>
|
|
|
|
</div>;
|
|
|
|
}
|
2015-08-28 15:22:09 +08:00
|
|
|
column.title = <div>
|
|
|
|
{column.title}
|
|
|
|
{sortButton}
|
|
|
|
{filterDropdown}
|
|
|
|
</div>;
|
2015-07-13 17:40:17 +08:00
|
|
|
return column;
|
2015-07-13 16:22:02 +08:00
|
|
|
});
|
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-09-23 14:28:08 +08:00
|
|
|
handleShowSizeChange(current, pageSize) {
|
|
|
|
let pagination = objectAssign(this.state.pagination, {
|
|
|
|
pageSize: pageSize
|
|
|
|
});
|
2015-10-22 17:27:53 +08:00
|
|
|
this.fetch({pagination});
|
2015-09-23 14:28:08 +08:00
|
|
|
},
|
|
|
|
|
2015-07-14 15:35:17 +08:00
|
|
|
renderPagination() {
|
|
|
|
// 强制不需要分页
|
2015-07-31 10:40:23 +08:00
|
|
|
if (!this.hasPagination()) {
|
|
|
|
return null;
|
2015-07-10 17:47:53 +08:00
|
|
|
}
|
2015-07-14 20:08:45 +08:00
|
|
|
let classString = 'ant-table-pagination';
|
|
|
|
if (this.props.size === 'small') {
|
|
|
|
classString += ' mini';
|
|
|
|
}
|
2015-08-28 17:30:37 +08:00
|
|
|
let total = this.state.pagination.total;
|
|
|
|
if (!total && this.isLocalDataSource()) {
|
2015-07-31 10:40:23 +08:00
|
|
|
total = this.getLocalData().length;
|
|
|
|
}
|
2015-08-28 17:30:37 +08:00
|
|
|
return (total > 0) ? <Pagination className={classString}
|
2015-10-22 17:27:53 +08:00
|
|
|
onChange={this.handlePageChange}
|
|
|
|
total={total}
|
|
|
|
pageSize={10}
|
|
|
|
onShowSizeChange={this.handleShowSizeChange}
|
2015-08-28 17:30:37 +08:00
|
|
|
{...this.state.pagination} /> : null;
|
2015-07-10 17:47:53 +08:00
|
|
|
},
|
2015-07-31 11:28:49 +08:00
|
|
|
|
2015-07-31 10:40:23 +08:00
|
|
|
prepareParamsArguments(state) {
|
2015-07-14 18:29:43 +08:00
|
|
|
// 准备筛选、排序、分页的参数
|
|
|
|
let pagination;
|
|
|
|
let filters = {};
|
2015-07-15 14:10:01 +08:00
|
|
|
let sorter = {};
|
2015-07-31 10:40:23 +08:00
|
|
|
pagination = state.pagination;
|
|
|
|
this.props.columns.forEach((column) => {
|
|
|
|
let colFilters = state.filters[this.getColumnKey(column)] || [];
|
|
|
|
if (colFilters.length > 0) {
|
|
|
|
filters[this.getColumnKey(column)] = colFilters;
|
2015-07-14 18:29:43 +08:00
|
|
|
}
|
|
|
|
});
|
2015-07-31 10:40:23 +08:00
|
|
|
if (state.sortColumn &&
|
|
|
|
state.sortOrder &&
|
|
|
|
state.sortColumn.dataIndex) {
|
|
|
|
sorter.field = state.sortColumn.dataIndex;
|
|
|
|
sorter.order = state.sortOrder;
|
2015-07-15 14:10:01 +08:00
|
|
|
}
|
|
|
|
return [pagination, filters, sorter];
|
2015-07-14 18:29:43 +08:00
|
|
|
},
|
2015-07-31 10:40:23 +08:00
|
|
|
|
|
|
|
fetch(newState) {
|
|
|
|
if (this.isLocalDataSource()) {
|
|
|
|
if (newState) {
|
|
|
|
this.setState(newState);
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-05 20:06:16 +08:00
|
|
|
// remote 模式使用 this.dataSource
|
|
|
|
let dataSource = this.getRemoteDataSource();
|
|
|
|
if (!dataSource) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
let state = objectAssign({}, this.state, newState);
|
|
|
|
if (newState || !this.state.loading) {
|
|
|
|
this.setState(objectAssign({
|
|
|
|
loading: true
|
|
|
|
}, newState));
|
|
|
|
}
|
2015-08-11 16:57:17 +08:00
|
|
|
let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
|
2015-08-18 14:30:55 +08:00
|
|
|
return reqwest({
|
2015-07-14 15:35:17 +08:00
|
|
|
url: dataSource.url,
|
2015-08-18 14:30:55 +08:00
|
|
|
method: 'get',
|
2015-08-11 16:57:17 +08:00
|
|
|
data: objectAssign(buildInParams, dataSource.data),
|
2015-07-14 20:04:14 +08:00
|
|
|
headers: dataSource.headers,
|
2015-08-18 14:30:55 +08:00
|
|
|
type: 'json',
|
2015-07-12 17:10:06 +08:00
|
|
|
success: (result) => {
|
|
|
|
if (this.isMounted()) {
|
2015-07-14 20:04:14 +08:00
|
|
|
let pagination = objectAssign(
|
2015-07-31 10:40:23 +08:00
|
|
|
state.pagination,
|
2015-07-14 17:58:00 +08:00
|
|
|
dataSource.getPagination.call(this, result)
|
|
|
|
);
|
2015-07-12 17:10:06 +08:00
|
|
|
this.setState({
|
2015-11-05 17:30:24 +08:00
|
|
|
selectionDirty: false,
|
2015-07-31 10:40:23 +08:00
|
|
|
loading: false,
|
2015-07-14 15:35:17 +08:00
|
|
|
data: dataSource.resolve.call(this, result),
|
2015-07-31 10:40:23 +08:00
|
|
|
pagination: pagination
|
2015-07-12 17:10:06 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2015-07-14 17:58:00 +08:00
|
|
|
error: () => {
|
2015-07-12 17:10:06 +08:00
|
|
|
this.setState({
|
2015-07-31 10:40:23 +08:00
|
|
|
loading: false,
|
|
|
|
data: []
|
2015-07-12 17:10:06 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-31 10:40:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-31 11:28:49 +08:00
|
|
|
findColumn(myKey) {
|
2015-07-31 10:40:23 +08:00
|
|
|
return this.props.columns.filter((c) => {
|
|
|
|
return this.getColumnKey(c) === myKey;
|
|
|
|
})[0];
|
|
|
|
},
|
|
|
|
|
2015-11-05 17:30:24 +08:00
|
|
|
getLocalDataPaging(dataSource) {
|
|
|
|
let data = this.getLocalData(dataSource);
|
2015-07-31 10:40:23 +08:00
|
|
|
let current, pageSize;
|
|
|
|
let state = this.state;
|
|
|
|
// 如果没有分页的话,默认全部展示
|
|
|
|
if (!this.hasPagination()) {
|
|
|
|
pageSize = Number.MAX_VALUE;
|
|
|
|
current = 1;
|
2015-07-14 15:35:17 +08:00
|
|
|
} else {
|
2015-07-31 10:40:23 +08:00
|
|
|
pageSize = state.pagination.pageSize;
|
|
|
|
current = state.pagination.current;
|
|
|
|
}
|
|
|
|
// 分页
|
|
|
|
// ---
|
|
|
|
// 当数据量少于每页数量时,直接设置数据
|
|
|
|
// 否则进行读取分页数据
|
|
|
|
if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
|
|
|
|
data = data.filter((item, i) => {
|
|
|
|
if (i >= (current - 1) * pageSize &&
|
|
|
|
i < current * pageSize) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
2015-11-05 17:30:24 +08:00
|
|
|
getLocalData(dataSource) {
|
2015-07-31 10:40:23 +08:00
|
|
|
let state = this.state;
|
2015-11-05 17:30:24 +08:00
|
|
|
let data = dataSource || this.state.dataSource;
|
2015-07-31 10:40:23 +08:00
|
|
|
// 排序
|
|
|
|
if (state.sortOrder && state.sorter) {
|
|
|
|
data = data.sort(state.sorter);
|
|
|
|
}
|
|
|
|
// 筛选
|
|
|
|
if (state.filters) {
|
|
|
|
Object.keys(state.filters).forEach((columnKey) => {
|
|
|
|
let col = this.findColumn(columnKey);
|
|
|
|
let values = state.filters[columnKey] || [];
|
2015-07-31 11:54:46 +08:00
|
|
|
if (values.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
data = data.filter((record) => {
|
|
|
|
return values.some((v)=> {
|
|
|
|
return col.onFilter(v, record);
|
|
|
|
});
|
2015-07-29 13:49:44 +08:00
|
|
|
});
|
2015-07-14 17:58:00 +08:00
|
|
|
});
|
2015-07-12 17:10:06 +08:00
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
return data;
|
2015-07-12 17:10:06 +08:00
|
|
|
},
|
2015-07-31 10:40:23 +08:00
|
|
|
|
2015-07-12 17:10:06 +08:00
|
|
|
componentDidMount() {
|
2015-07-31 10:40:23 +08:00
|
|
|
if (!this.isLocalDataSource()) {
|
|
|
|
this.fetch();
|
|
|
|
}
|
2015-07-12 17:10:06 +08:00
|
|
|
},
|
2015-07-14 15:35:17 +08:00
|
|
|
|
2015-07-31 10:40:23 +08:00
|
|
|
render() {
|
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
let columns = this.renderRowSelection();
|
2015-10-22 17:27:53 +08:00
|
|
|
let classString = this.props.className;
|
2015-09-10 11:04:31 +08:00
|
|
|
let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
|
2015-07-12 18:14:17 +08:00
|
|
|
if (this.props.size === 'small') {
|
|
|
|
classString += ' ant-table-small';
|
|
|
|
}
|
2015-08-11 20:05:52 +08:00
|
|
|
if (this.props.bordered) {
|
|
|
|
classString += ' ant-table-bordered';
|
|
|
|
}
|
2015-07-31 10:40:23 +08:00
|
|
|
columns = this.renderColumnsDropdown(columns);
|
2015-08-28 15:22:09 +08:00
|
|
|
columns = columns.map((column, i) => {
|
|
|
|
column.key = column.dataIndex || i;
|
|
|
|
return column;
|
|
|
|
});
|
2015-08-28 17:20:02 +08:00
|
|
|
let emptyText;
|
2015-09-15 11:40:58 +08:00
|
|
|
let emptyClass = '';
|
2015-08-28 17:20:02 +08:00
|
|
|
if (!data || data.length === 0) {
|
2015-09-15 11:40:58 +08:00
|
|
|
emptyText = <div className="ant-table-placeholder">
|
2015-10-22 17:27:53 +08:00
|
|
|
<Icon type="frown"/>暂无数据
|
2015-08-28 17:20:02 +08:00
|
|
|
</div>;
|
2015-09-15 11:40:58 +08:00
|
|
|
emptyClass = ' ant-table-empty';
|
2015-08-28 17:20:02 +08:00
|
|
|
}
|
2015-10-31 02:12:26 +08:00
|
|
|
|
2015-11-05 20:02:49 +08:00
|
|
|
let table = <div>
|
2015-11-03 17:48:56 +08:00
|
|
|
<Table {...this.props}
|
|
|
|
data={data}
|
|
|
|
columns={columns}
|
|
|
|
className={classString}
|
|
|
|
expandIconAsCell={expandIconAsCell} />
|
2015-11-05 20:02:49 +08:00
|
|
|
{emptyText}
|
|
|
|
</div>;
|
2015-11-03 18:16:16 +08:00
|
|
|
if (this.state.loading) {
|
2015-10-27 10:34:05 +08:00
|
|
|
// if there is no pagination or no data, the height of spin should decrease by half of pagination
|
|
|
|
let paginationPatchClass = (this.hasPagination() && data && data.length !== 0)
|
|
|
|
? 'ant-table-with-pagination'
|
|
|
|
: 'ant-table-without-pagination';
|
2015-10-31 02:12:26 +08:00
|
|
|
let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
|
2015-11-03 17:48:56 +08:00
|
|
|
table = <Spin className={spinClassName}>{table}</Spin>;
|
2015-10-31 02:12:26 +08:00
|
|
|
}
|
2015-11-03 17:48:56 +08:00
|
|
|
return (
|
|
|
|
<div className={'clearfix' + emptyClass}>
|
|
|
|
{table}
|
|
|
|
{this.renderPagination()}
|
|
|
|
</div>
|
|
|
|
);
|
2015-07-09 11:41:36 +08:00
|
|
|
}
|
|
|
|
});
|
2015-08-05 01:42:37 +08:00
|
|
|
|
|
|
|
AntTable.DataSource = DataSource;
|
|
|
|
|
2015-08-05 01:44:21 +08:00
|
|
|
export default AntTable;
|