You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
18 KiB

10 years ago
import React from 'react';
9 years ago
import reqwest from 'reqwest';
10 years ago
import Table from 'rc-table';
import Checkbox from '../checkbox';
import Radio from '../radio';
import FilterDropdown from './filterDropdown';
import Pagination from '../pagination';
import Icon from '../iconfont';
import objectAssign from 'object-assign';
import Spin from '../spin';
10 years ago
9 years ago
function noop() {
}
9 years ago
function defaultResolve(data) {
return data || [];
}
class DataSource {
9 years ago
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 || {};
9 years ago
}
constructor(config) {
if (config) {
this.init(config);
}
}
clone(config = {}) {
return new DataSource(objectAssign({}, this.config, config));
}
}
let AntTable = React.createClass({
9 years ago
getInitialState() {
10 years ago
return {
9 years ago
// 减少状态
selectedRowKeys: [],
9 years ago
// only for remote
data: [],
dataSource: this.props.dataSource,
9 years ago
filters: {},
dirty: false,
loading: false,
9 years ago
sortColumn: '',
sortOrder: '',
sorter: null,
radioIndex: null,
9 years ago
pagination: this.hasPagination() ? objectAssign({
pageSize: 10,
current: 1
9 years ago
}, this.props.pagination) : {}
10 years ago
};
},
9 years ago
10 years ago
getDefaultProps() {
return {
prefixCls: 'ant-table',
10 years ago
useFixedHeader: false,
10 years ago
rowSelection: null,
className: '',
size: 'default',
bordered: false,
onChange: function () {
}
10 years ago
};
},
9 years ago
propTypes: {
dataSource: React.PropTypes.oneOfType([React.PropTypes.array, React.PropTypes.instanceOf(DataSource)])
},
getDefaultSelection() {
let selectedRowKeys = [];
if (this.props.rowSelection && this.props.rowSelection.getCheckboxProps) {
let data = this.getCurrentPageData();
data.filter((item) => {
if (this.props.rowSelection.getCheckboxProps) {
return this.props.rowSelection.getCheckboxProps(item).defaultValue;
}
return true;
}).map((record, rowIndex) => {
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
});
}
return selectedRowKeys;
},
9 years ago
componentWillReceiveProps(nextProps) {
9 years ago
if (('pagination' in nextProps) && nextProps.pagination !== false) {
this.setState({
pagination: objectAssign({}, this.state.pagination, nextProps.pagination)
});
}
// 外界只有 dataSource 的变化会触发新请求
if ('dataSource' in nextProps &&
nextProps.dataSource !== this.props.dataSource) {
this.setState({
selectedRowKeys: [],
dataSource: nextProps.dataSource,
loading: true
}, this.fetch);
9 years ago
}
if (nextProps.columns !== this.props.columns) {
this.setState({
filters: {}
});
}
},
9 years ago
hasPagination(pagination) {
9 years ago
if (pagination === undefined) {
pagination = this.props.pagination;
}
9 years ago
return pagination !== false;
},
9 years ago
isLocalDataSource() {
return Array.isArray(this.state.dataSource);
},
9 years ago
getRemoteDataSource() {
return this.state.dataSource;
},
9 years ago
toggleSortOrder(order, column) {
let sortColumn = this.state.sortColumn;
let sortOrder = this.state.sortOrder;
9 years ago
let sorter;
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
let isSortColumn = this.isSortColumn(column);
if (!isSortColumn) { // 当前列未排序
sortOrder = order;
sortColumn = column;
} else { // 当前列已排序
if (sortOrder === order) { // 切换为未排序状态
sortOrder = '';
sortColumn = null;
} else { // 切换为排序状态
sortOrder = order;
}
}
9 years ago
if (this.isLocalDataSource()) {
sorter = function () {
let result = column.sorter.apply(this, arguments);
if (sortOrder === 'ascend') {
return result;
} else if (sortOrder === 'descend') {
return -result;
}
};
}
const newState = {
sortOrder,
sortColumn,
sorter
};
this.fetch(newState);
this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
},
9 years ago
9 years ago
handleFilter(column, filters) {
filters = objectAssign({}, this.state.filters, {
[this.getColumnKey(column)]: filters
});
const newState = {
9 years ago
selectedRowKeys: [],
filters
};
this.fetch(newState);
this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
10 years ago
},
9 years ago
9 years ago
handleSelect(record, rowIndex, e) {
let checked = e.target.checked;
let defaultSelection = [];
if (!this.state.dirty) {
defaultSelection = this.getDefaultSelection();
}
let selectedRowKeys = this.state.selectedRowKeys.concat(defaultSelection);
9 years ago
let key = this.getRecordKey(record, rowIndex);
if (checked) {
9 years ago
selectedRowKeys.push(this.getRecordKey(record, rowIndex));
} else {
9 years ago
selectedRowKeys = selectedRowKeys.filter((i) => {
return key !== i;
});
}
this.setState({
selectedRowKeys: selectedRowKeys,
dirty: true
});
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 = [];
if (!this.state.dirty) {
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,
dirty: true
});
if (this.props.rowSelection.onSelect) {
9 years ago
let data = this.getCurrentPageData();
let selectedRows = data.filter((row, i) => {
return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
10 years ago
});
9 years ago
this.props.rowSelection.onSelect(record, checked, selectedRows);
}
},
9 years ago
handleSelectAllRow(e) {
let checked = e.target.checked;
9 years ago
let data = this.getCurrentPageData();
let selectedRowKeys = checked ? data.filter((item) => {
if (this.props.rowSelection.getCheckboxProps) {
return !this.props.rowSelection.getCheckboxProps(item).disabled;
}
return true;
}).map((item, i) => {
9 years ago
return this.getRecordKey(item, i);
}) : [];
10 years ago
this.setState({
selectedRowKeys: selectedRowKeys,
dirty: true
});
if (this.props.rowSelection.onSelectAll) {
9 years ago
let selectedRows = data.filter((row, i) => {
return selectedRowKeys.indexOf(this.getRecordKey(row, i)) >= 0;
10 years ago
});
this.props.rowSelection.onSelectAll(checked, selectedRows);
10 years ago
}
},
9 years ago
handlePageChange(current) {
9 years ago
let pagination = objectAssign({}, this.state.pagination);
if (current) {
pagination.current = current;
} else {
pagination.current = pagination.current || 1;
}
const newState = {
9 years ago
// 防止内存泄漏,只维持当页
selectedRowKeys: [],
pagination
};
this.fetch(newState);
this.props.onChange.apply(this, this.prepareParamsArguments(objectAssign({}, this.state, newState)));
},
9 years ago
onRadioChange: function (ev) {
this.setState({
radioIndex: ev.target.value
9 years ago
});
},
9 years ago
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);
}
const checked = this.state.dirty ? this.state.radioIndex === record.key : this.getDefaultSelection().indexOf(rowIndex) >= 0;
return <Radio disabled={props.disabled} onChange={this.handleRadioSelect.bind(this, record, rowIndex)}
value={record.key} checked={checked}/>;
},
renderSelectionCheckBox(value, record, index) {
9 years ago
let rowIndex = this.getRecordKey(record, index); // 从 1 开始
let checked = this.state.dirty ? this.state.selectedRowKeys.indexOf(rowIndex) >= 0 : this.getDefaultSelection().indexOf(rowIndex) >= 0;
let props = {};
if (this.props.rowSelection.getCheckboxProps) {
props = this.props.rowSelection.getCheckboxProps.call(this, record);
}
return <Checkbox checked={checked} disabled={props.disabled}
onChange={this.handleSelect.bind(this, record, rowIndex)}/>;
9 years ago
},
9 years ago
getRecordKey(record, index) {
9 years ago
return record.key || index;
},
9 years ago
renderRowSelection() {
9 years ago
let columns = this.props.columns.concat();
if (this.props.rowSelection) {
9 years ago
let data = this.getCurrentPageData();
let checked;
if (!data.length) {
checked = false;
} else {
checked = data.filter((item) => {
if (this.props.rowSelection.getCheckboxProps) {
return !this.props.rowSelection.getCheckboxProps(item).disabled;
}
return true;
}).every((item, i) => {
9 years ago
let key = this.getRecordKey(item, i);
return this.state.selectedRowKeys.indexOf(key) >= 0;
});
}
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'
};
}
if (columns[0] &&
9 years ago
columns[0].key === 'selection-column') {
columns[0] = selectionColumn;
} else {
columns.unshift(selectionColumn);
}
}
return columns;
},
9 years ago
9 years ago
getCurrentPageData() {
9 years ago
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;
9 years ago
},
renderColumnsDropdown(columns) {
9 years ago
return columns.map((column, i) => {
column = objectAssign({}, column);
let key = this.getColumnKey(column, i);
let filterDropdown, sortButton;
if (column.filters && column.filters.length > 0) {
9 years ago
let colFilters = this.state.filters[key] || [];
filterDropdown =
<FilterDropdown column={column}
selectedKeys={colFilters}
confirmFilter={this.handleFilter}/>;
}
if (column.sorter) {
let isSortColumn = this.isSortColumn(column);
9 years ago
if (isSortColumn) {
column.className = column.className || '';
if (this.state.sortOrder) {
column.className += ' ant-table-column-sort';
}
9 years ago
}
sortButton = <div className="ant-table-column-sorter">
<span className={'ant-table-column-sorter-up ' +
((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
9 years ago
title="升序排序"
onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
<Icon type="caret-up"/>
</span>
<span className={'ant-table-column-sorter-down ' +
((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
9 years ago
title="降序排序"
onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
<Icon type="caret-down"/>
</span>
</div>;
}
column.title = <div>
{column.title}
{sortButton}
{filterDropdown}
</div>;
return column;
});
},
9 years ago
handleShowSizeChange(current, pageSize) {
let pagination = objectAssign(this.state.pagination, {
pageSize: pageSize
});
this.fetch({pagination});
},
renderPagination() {
// 强制不需要分页
9 years ago
if (!this.hasPagination()) {
return null;
10 years ago
}
let classString = 'ant-table-pagination';
if (this.props.size === 'small') {
classString += ' mini';
}
let total = this.state.pagination.total;
if (!total && this.isLocalDataSource()) {
9 years ago
total = this.getLocalData().length;
}
return (total > 0) ? <Pagination className={classString}
onChange={this.handlePageChange}
total={total}
pageSize={10}
onShowSizeChange={this.handleShowSizeChange}
{...this.state.pagination} /> : null;
10 years ago
},
9 years ago
9 years ago
prepareParamsArguments(state) {
// 准备筛选、排序、分页的参数
let pagination;
let filters = {};
10 years ago
let sorter = {};
9 years ago
pagination = state.pagination;
this.props.columns.forEach((column) => {
let colFilters = state.filters[this.getColumnKey(column)] || [];
if (colFilters.length > 0) {
filters[this.getColumnKey(column)] = colFilters;
}
});
9 years ago
if (state.sortColumn &&
state.sortOrder &&
state.sortColumn.dataIndex) {
sorter.field = state.sortColumn.dataIndex;
sorter.order = state.sortOrder;
10 years ago
}
return [pagination, filters, sorter];
},
9 years ago
fetch(newState) {
if (this.isLocalDataSource()) {
if (newState) {
this.setState(newState);
}
} else {
let state = objectAssign({}, this.state, newState);
if (newState || !this.state.loading) {
this.setState(objectAssign({
loading: true
}, newState));
}
// remote 模式使用 this.dataSource
9 years ago
let dataSource = this.getRemoteDataSource();
let buildInParams = dataSource.getParams.apply(this, this.prepareParamsArguments(state)) || {};
return reqwest({
url: dataSource.url,
method: 'get',
data: objectAssign(buildInParams, dataSource.data),
headers: dataSource.headers,
type: 'json',
success: (result) => {
if (this.isMounted()) {
let pagination = objectAssign(
9 years ago
state.pagination,
10 years ago
dataSource.getPagination.call(this, result)
);
this.setState({
dirty: false,
9 years ago
loading: false,
data: dataSource.resolve.call(this, result),
9 years ago
pagination: pagination
});
}
},
10 years ago
error: () => {
this.setState({
9 years ago
loading: false,
data: []
});
}
});
9 years ago
}
},
9 years ago
findColumn(myKey) {
9 years ago
return this.props.columns.filter((c) => {
return this.getColumnKey(c) === myKey;
})[0];
},
9 years ago
getLocalDataPaging() {
9 years ago
let data = this.getLocalData();
let current, pageSize;
let state = this.state;
// 如果没有分页的话,默认全部展示
if (!this.hasPagination()) {
pageSize = Number.MAX_VALUE;
current = 1;
} else {
9 years ago
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;
},
9 years ago
getLocalData() {
9 years ago
let state = this.state;
let data = this.state.dataSource;
9 years ago
// 排序
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] || [];
if (values.length === 0) {
return;
}
9 years ago
data = data.filter((record) => {
return values.some((v)=> {
return col.onFilter(v, record);
});
});
10 years ago
});
}
9 years ago
return data;
},
9 years ago
componentDidMount() {
9 years ago
if (!this.isLocalDataSource()) {
this.fetch();
}
},
9 years ago
render() {
let data = this.getCurrentPageData();
let columns = this.renderRowSelection();
let classString = this.props.className;
9 years ago
let expandIconAsCell = this.props.expandedRowRender && this.props.expandIconAsCell !== false;
10 years ago
if (this.props.size === 'small') {
classString += ' ant-table-small';
}
9 years ago
if (this.props.bordered) {
classString += ' ant-table-bordered';
}
9 years ago
columns = this.renderColumnsDropdown(columns);
columns = columns.map((column, i) => {
column.key = column.dataIndex || i;
return column;
});
let emptyText;
let emptyClass = '';
if (!data || data.length === 0) {
emptyText = <div className="ant-table-placeholder">
<Icon type="frown"/>暂无数据
</div>;
emptyClass = ' ant-table-empty';
}
const table = (
<div className={'clearfix' + emptyClass}>
<Table {...this.props}
data={data}
columns={columns}
className={classString}
expandIconAsCell={expandIconAsCell} />
{emptyText}
{this.renderPagination()}
</div>
);
if (this.state.loading && !this.isLocalDataSource()) {
// 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';
let spinClassName = `${paginationPatchClass} ant-table-spin-holder`;
return <Spin className={spinClassName}>{table}</Spin>;
}
return table;
10 years ago
}
});
AntTable.DataSource = DataSource;
export default AntTable;