|
|
|
import React from 'react';
|
|
|
|
import reqwest from 'reqwest-without-xhr2';
|
|
|
|
import Table from 'rc-table';
|
|
|
|
import Checkbox from '../checkbox';
|
|
|
|
import FilterDropdown from './filterDropdown';
|
|
|
|
import Pagination from '../pagination';
|
|
|
|
import objectAssign from 'object-assign';
|
|
|
|
|
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultResolve(data) {
|
|
|
|
return data || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataSource {
|
|
|
|
init(config) {
|
|
|
|
this.config = config;
|
|
|
|
this.url = config.url || '';
|
|
|
|
this.resolve = config.resolve || defaultResolve;
|
|
|
|
this.getParams = config.getParams || noop;
|
|
|
|
this.getPagination = config.getPagination || noop;
|
|
|
|
this.headers = config.headers || {};
|
|
|
|
this.data = config.data || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(config) {
|
|
|
|
if (config) {
|
|
|
|
this.init(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clone(config = {}) {
|
|
|
|
return new DataSource(objectAssign({}, this.config, config));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var AntTable = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
// 减少状态
|
|
|
|
selectedRowKeys: [],
|
|
|
|
// only for remote
|
|
|
|
data: [],
|
|
|
|
dataSource: this.props.dataSource,
|
|
|
|
filters: {},
|
|
|
|
loading: false,
|
|
|
|
sortColumn: '',
|
|
|
|
sortOrder: '',
|
|
|
|
sorter: null,
|
|
|
|
pagination: this.hasPagination() ? objectAssign({
|
|
|
|
pageSize: 10,
|
|
|
|
current: 1
|
|
|
|
}, this.props.pagination) : {}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
prefixCls: 'ant-table',
|
|
|
|
useFixedHeader: false,
|
|
|
|
rowSelection: null,
|
|
|
|
size: 'normal',
|
|
|
|
bordered: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
let newState = {};
|
|
|
|
if (('pagination' in nextProps) && nextProps.pagination !== false) {
|
|
|
|
newState.pagination = objectAssign({}, this.state.pagination, nextProps.pagination);
|
|
|
|
}
|
|
|
|
// 外界只有 dataSource 的变化会触发新请求
|
|
|
|
if ('dataSource' in nextProps &&
|
|
|
|
nextProps.dataSource !== this.props.dataSource) {
|
|
|
|
newState = {
|
|
|
|
selectedRowKeys: [],
|
|
|
|
dataSource: nextProps.dataSource,
|
|
|
|
loading: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (nextProps.columns !== this.props.columns) {
|
|
|
|
newState.filters = {};
|
|
|
|
}
|
|
|
|
this.setState(newState, this.fetch);
|
|
|
|
},
|
|
|
|
|
|
|
|
hasPagination(pagination) {
|
|
|
|
if (pagination === undefined) {
|
|
|
|
pagination = this.props.pagination;
|
|
|
|
}
|
|
|
|
return pagination !== false;
|
|
|
|
},
|
|
|
|
|
|
|
|
isLocalDataSource() {
|
|
|
|
return Array.isArray(this.state.dataSource);
|
|
|
|
},
|
|
|
|
|
|
|
|
getRemoteDataSource() {
|
|
|
|
return this.state.dataSource;
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleSortOrder(order, column) {
|
|
|
|
let sortColumn = this.state.sortColumn;
|
|
|
|
let sortOrder = this.state.sortOrder;
|
|
|
|
let sorter;
|
|
|
|
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
|
|
|
let isSortColumn = this.isSortColumn(column);
|
|
|
|
if (!isSortColumn) { // 当前列未排序
|
|
|
|
sortOrder = order;
|
|
|
|
sortColumn = column;
|
|
|
|
} else { // 当前列已排序
|
|
|
|
if (sortOrder === order) { // 切换为未排序状态
|
|
|
|
sortOrder = '';
|
|
|
|
sortColumn = null;
|
|
|
|
} else { // 切换为排序状态
|
|
|
|
sortOrder = order;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.isLocalDataSource()) {
|
|
|
|
sorter = function () {
|
|
|
|
let result = column.sorter.apply(this, arguments);
|
|
|
|
if (sortOrder === 'ascend') {
|
|
|
|
return result;
|
|
|
|
} else if (sortOrder === 'descend') {
|
|
|
|
return -result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
this.fetch({
|
|
|
|
sortOrder: sortOrder,
|
|
|
|
sortColumn: sortColumn,
|
|
|
|
sorter: sorter
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleFilter(column, filters) {
|
|
|
|
filters = objectAssign({}, this.state.filters, {
|
|
|
|
[this.getColumnKey(column)]: filters
|
|
|
|
});
|
|
|
|
this.fetch({
|
|
|
|
selectedRowKeys: [],
|
|
|
|
filters: filters
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleSelect(record, rowIndex, e) {
|
|
|
|
let checked = e.target.checked;
|
|
|
|
let selectedRowKeys = this.state.selectedRowKeys.concat();
|
|
|
|
let key = this.getRecordKey(record, rowIndex);
|
|
|
|
if (checked) {
|
|
|
|
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
|
|
|
|
} else {
|
|
|
|
selectedRowKeys = selectedRowKeys.filter((i) => {
|
|
|
|
return key !== i;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
selectedRowKeys: selectedRowKeys
|
|
|
|
});
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleSelectAllRow(e) {
|
|
|
|
let checked = e.target.checked;
|
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
let selectedRowKeys = checked ? data.map((item, i) => {
|
|
|
|
return this.getRecordKey(item, i);
|
|
|
|
}) : [];
|
|
|
|
this.setState({
|
|
|
|
selectedRowKeys: selectedRowKeys
|
|
|
|
});
|
|
|
|
if (this.props.rowSelection.onSelectAll) {
|
|
|
|
let selectedRows = data.filter((row, i) => {
|
|
|
|
return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
|
|
|
|
});
|
|
|
|
this.props.rowSelection.onSelectAll(checked, selectedRows);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handlePageChange(current) {
|
|
|
|
let pagination = objectAssign({}, this.state.pagination);
|
|
|
|
if (current) {
|
|
|
|
pagination.current = current;
|
|
|
|
} else {
|
|
|
|
pagination.current = pagination.current || 1;
|
|
|
|
}
|
|
|
|
this.fetch({
|
|
|
|
// 防止内存泄漏,只维持当页
|
|
|
|
selectedRowKeys: [],
|
|
|
|
pagination: pagination
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
renderSelectionCheckBox(value, record, index) {
|
|
|
|
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
|
|
|
|
let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
|
|
|
|
return <Checkbox checked={checked} onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
|
|
|
|
},
|
|
|
|
|
|
|
|
getRecordKey(record, index) {
|
|
|
|
return record.key || index;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderRowSelection() {
|
|
|
|
let columns = this.props.columns.concat();
|
|
|
|
if (this.props.rowSelection) {
|
|
|
|
let data = this.getCurrentPageData();
|
|
|
|
let checked;
|
|
|
|
if (!data.length) {
|
|
|
|
checked = false;
|
|
|
|
} else {
|
|
|
|
checked = data.every((item, i) => {
|
|
|
|
let key = this.getRecordKey(item, i);
|
|
|
|
return this.state.selectedRowKeys.indexOf(key) >= 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow}/>;
|
|
|
|
let selectionColumn = {
|
|
|
|
key: 'selection-column',
|
|
|
|
title: checkboxAll,
|
|
|
|
width: 60,
|
|
|
|
render: this.renderSelectionCheckBox,
|
|
|
|
className: 'ant-table-selection-column'
|
|
|
|
};
|
|
|
|
if (columns[0] &&
|
|
|
|
columns[0].key === 'selection-column') {
|
|
|
|
columns[0] = selectionColumn;
|
|
|
|
} else {
|
|
|
|
columns.unshift(selectionColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentPageData() {
|
|
|
|
return this.isLocalDataSource() ? this.getLocalDataPaging() : this.state.data;
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|