2015-07-09 11:41:36 +08:00
|
|
|
import React from 'react';
|
2015-07-12 17:10:06 +08:00
|
|
|
import jQuery from 'jquery';
|
2015-07-09 11:41:36 +08:00
|
|
|
import Table from 'rc-table';
|
2015-07-13 16:22:02 +08:00
|
|
|
import Dropdown from '../dropdown';
|
2015-07-15 17:22:58 +08:00
|
|
|
import Checkbox from '../checkbox';
|
2015-07-15 12:16:28 +08:00
|
|
|
import FilterMenu from './filterMenu';
|
2015-07-14 15:35:17 +08:00
|
|
|
import Pagination from '../pagination';
|
2015-07-14 20:04:14 +08:00
|
|
|
import objectAssign from 'object-assign';
|
2015-07-09 11:41:36 +08:00
|
|
|
|
2015-07-23 22:23:11 +08:00
|
|
|
export default React.createClass({
|
2015-07-10 17:47:53 +08:00
|
|
|
getInitialState() {
|
2015-07-28 15:20:15 +08:00
|
|
|
this.initDataSource(this.props.dataSource);
|
2015-07-27 19:58:51 +08:00
|
|
|
|
|
|
|
let noPagination = (this.props.pagination === false);
|
2015-07-28 14:02:18 +08:00
|
|
|
let pagination = this.initPagination(this.props.pagination);
|
2015-07-27 19:58:51 +08:00
|
|
|
|
2015-07-10 17:47:53 +08:00
|
|
|
return {
|
2015-07-12 17:10:06 +08:00
|
|
|
selectedRowKeys: [],
|
|
|
|
loading: false,
|
2015-07-14 15:35:17 +08:00
|
|
|
pagination: pagination,
|
2015-07-27 19:58:51 +08:00
|
|
|
noPagination: noPagination,
|
2015-07-15 14:10:01 +08:00
|
|
|
data: []
|
2015-07-10 17:47:53 +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,
|
|
|
|
size: 'normal'
|
2015-07-09 11:41:36 +08:00
|
|
|
};
|
|
|
|
},
|
2015-07-28 14:02:18 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if ('pagination' in nextProps) {
|
|
|
|
let noPagination = (nextProps.pagination === false);
|
|
|
|
this.setState({
|
|
|
|
pagination: this.initPagination(nextProps.pagination),
|
|
|
|
noPagination: noPagination
|
|
|
|
});
|
|
|
|
}
|
2015-07-28 15:20:15 +08:00
|
|
|
if ('dataSource' in nextProps) {
|
|
|
|
this.initDataSource(nextProps.dataSource);
|
|
|
|
this.setState({
|
|
|
|
data: nextProps.dataSource
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
initDataSource(dataSource) {
|
|
|
|
// 支持两种模式
|
|
|
|
if (Array.isArray(dataSource)) {
|
|
|
|
this.mode = 'local';
|
|
|
|
// 保留原来的数据
|
|
|
|
this.originDataSource = dataSource.slice(0);
|
|
|
|
} else {
|
|
|
|
this.mode = 'remote';
|
|
|
|
this.dataSource = objectAssign({
|
|
|
|
resolve: function(data) {
|
|
|
|
return data || [];
|
|
|
|
},
|
|
|
|
getParams: function() {},
|
|
|
|
getPagination: function() {}
|
|
|
|
}, dataSource);
|
|
|
|
}
|
2015-07-28 14:02:18 +08:00
|
|
|
},
|
|
|
|
initPagination(pagination) {
|
|
|
|
return objectAssign({
|
|
|
|
pageSize: 10,
|
|
|
|
total: this.props.dataSource.length
|
|
|
|
}, pagination);
|
|
|
|
},
|
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-07-15 11:43:00 +08:00
|
|
|
// 同时允许一列进行排序,否则会导致排序顺序的逻辑问题
|
|
|
|
if (sortColumn) {
|
|
|
|
sortColumn.className = '';
|
|
|
|
}
|
|
|
|
if (sortColumn !== column) { // 当前列未排序
|
|
|
|
sortOrder = order;
|
|
|
|
sortColumn = column;
|
|
|
|
sortColumn.className = 'ant-table-column-sort';
|
|
|
|
} else { // 当前列已排序
|
|
|
|
if (sortOrder === order) { // 切换为未排序状态
|
|
|
|
sortOrder = '';
|
|
|
|
sortColumn = null;
|
|
|
|
} else { // 切换为排序状态
|
|
|
|
sortOrder = order;
|
|
|
|
sortColumn.className = 'ant-table-column-sort';
|
|
|
|
}
|
2015-07-13 16:22:02 +08:00
|
|
|
}
|
2015-07-14 15:35:17 +08:00
|
|
|
if (this.mode === 'local') {
|
2015-07-16 18:26:17 +08:00
|
|
|
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-07-15 14:10:01 +08:00
|
|
|
this.setState({
|
|
|
|
sortOrder: sortOrder,
|
2015-07-16 18:26:17 +08:00
|
|
|
sortColumn: sortColumn,
|
|
|
|
sorter: sorter
|
2015-07-15 14:10:01 +08:00
|
|
|
}, this.fetch);
|
2015-07-13 16:22:02 +08:00
|
|
|
},
|
2015-07-13 18:42:08 +08:00
|
|
|
handleFilter(column) {
|
2015-07-16 18:26:17 +08:00
|
|
|
let columnIndex = this.props.columns.indexOf(column);
|
|
|
|
let filterFns = [];
|
2015-07-14 18:29:43 +08:00
|
|
|
if (this.mode === 'local') {
|
2015-07-16 18:26:17 +08:00
|
|
|
filterFns[columnIndex] = function(record) {
|
2015-07-14 18:29:43 +08:00
|
|
|
if (column.selectedFilters.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return column.selectedFilters.some(function(value) {
|
|
|
|
return column.onFilter.call(this, value, record);
|
|
|
|
});
|
2015-07-16 18:26:17 +08:00
|
|
|
};
|
2015-07-14 18:29:43 +08:00
|
|
|
}
|
2015-07-16 18:26:17 +08:00
|
|
|
this.setState({
|
|
|
|
filterFns: filterFns
|
|
|
|
}, this.fetch);
|
2015-07-13 18:42:08 +08:00
|
|
|
},
|
2015-07-16 14:41:28 +08:00
|
|
|
handleSelect(rowIndex, e) {
|
|
|
|
let checked = e.target.checked;
|
2015-07-14 15:35:17 +08:00
|
|
|
if (checked) {
|
2015-07-15 17:22:58 +08:00
|
|
|
this.state.selectedRowKeys.push(rowIndex);
|
2015-07-14 15:35:17 +08:00
|
|
|
} else {
|
2015-07-15 11:12:17 +08:00
|
|
|
this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i) {
|
2015-07-15 17:22:58 +08:00
|
|
|
return rowIndex !== i;
|
2015-07-14 15:35:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
selectedRowKeys: this.state.selectedRowKeys
|
|
|
|
});
|
|
|
|
if (this.props.rowSelection.onSelect) {
|
2015-07-15 20:22:56 +08:00
|
|
|
let currentRow = this.state.data[rowIndex - 1];
|
|
|
|
let selectedRows = this.state.data.filter((row, i) => {
|
|
|
|
return this.state.selectedRowKeys.indexOf(i + 1) >= 0;
|
|
|
|
});
|
|
|
|
this.props.rowSelection.onSelect(currentRow, checked, selectedRows);
|
2015-07-14 15:35:17 +08:00
|
|
|
}
|
|
|
|
},
|
2015-07-16 14:41:28 +08:00
|
|
|
handleSelectAllRow(e) {
|
|
|
|
let checked = e.target.checked;
|
|
|
|
let selectedRowKeys = checked ? this.state.data.map(function(item, i) {
|
2015-07-14 15:35:17 +08:00
|
|
|
return i + 1;
|
2015-07-15 20:22:56 +08:00
|
|
|
}) : [];
|
|
|
|
this.setState({
|
|
|
|
selectedRowKeys: selectedRowKeys
|
2015-07-14 15:35:17 +08:00
|
|
|
});
|
|
|
|
if (this.props.rowSelection.onSelectAll) {
|
2015-07-15 20:22:56 +08:00
|
|
|
let selectedRows = this.state.data.filter((row, i) => {
|
|
|
|
return selectedRowKeys.indexOf(i + 1) >= 0;
|
|
|
|
});
|
|
|
|
this.props.rowSelection.onSelectAll(checked, selectedRows);
|
2015-07-13 18:42:08 +08:00
|
|
|
}
|
2015-07-13 17:40:17 +08:00
|
|
|
},
|
2015-07-28 12:26:54 +08:00
|
|
|
handlePageChange(current) {
|
2015-07-27 19:23:51 +08:00
|
|
|
let pagination = this.state.pagination || {};
|
2015-07-28 12:26:54 +08:00
|
|
|
if (current) {
|
|
|
|
pagination.current = current;
|
|
|
|
} else {
|
|
|
|
pagination.current = pagination.current || 1;
|
|
|
|
}
|
2015-07-15 14:10:01 +08:00
|
|
|
this.setState({
|
|
|
|
pagination: pagination
|
|
|
|
}, this.fetch);
|
2015-07-14 15:35:17 +08:00
|
|
|
},
|
|
|
|
renderSelectionCheckBox(value, record, index) {
|
2015-07-15 17:22:58 +08:00
|
|
|
let rowIndex = index + 1; // 从 1 开始
|
|
|
|
let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
|
2015-07-15 17:55:30 +08:00
|
|
|
return <Checkbox checked={checked} onChange={this.handleSelect.bind(this, rowIndex)} />;
|
2015-07-14 15:35:17 +08:00
|
|
|
},
|
|
|
|
renderRowSelection() {
|
|
|
|
var columns = this.props.columns;
|
|
|
|
if (this.props.rowSelection) {
|
|
|
|
let checked = this.state.data.every(function(item, i) {
|
|
|
|
return this.state.selectedRowKeys.indexOf(i + 1) >= 0;
|
|
|
|
}, this);
|
2015-07-15 17:55:30 +08:00
|
|
|
let checkboxAll = <Checkbox checked={checked} onChange={this.handleSelectAllRow} />;
|
2015-07-14 15:35:17 +08:00
|
|
|
let selectionColumn = {
|
|
|
|
key: 'selection-column',
|
|
|
|
title: checkboxAll,
|
|
|
|
width: 60,
|
2015-07-15 11:12:17 +08:00
|
|
|
render: this.renderSelectionCheckBox,
|
|
|
|
className: 'ant-table-selection-column'
|
2015-07-14 15:35:17 +08:00
|
|
|
};
|
|
|
|
if (columns[0] &&
|
|
|
|
columns[0].key === 'selection-column') {
|
|
|
|
columns[0] = selectionColumn;
|
|
|
|
} else {
|
|
|
|
columns.unshift(selectionColumn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
},
|
2015-07-13 16:22:02 +08:00
|
|
|
renderColumnsDropdown() {
|
2015-07-14 15:35:17 +08:00
|
|
|
return this.props.columns.map((column) => {
|
2015-07-13 16:22:02 +08:00
|
|
|
if (!column.originTitle) {
|
|
|
|
column.originTitle = column.title;
|
|
|
|
}
|
|
|
|
let filterDropdown, menus, sortButton;
|
2015-07-14 15:35:17 +08:00
|
|
|
if (column.filters && column.filters.length > 0) {
|
2015-07-14 20:43:27 +08:00
|
|
|
column.selectedFilters = column.selectedFilters || [];
|
2015-07-15 12:16:28 +08:00
|
|
|
menus = <FilterMenu column={column} confirmFilter={this.handleFilter.bind(this, column)} />;
|
2015-07-13 18:42:08 +08:00
|
|
|
let dropdownSelectedClass = '';
|
2015-07-14 15:35:17 +08:00
|
|
|
if (column.selectedFilters && column.selectedFilters.length > 0) {
|
2015-07-13 18:42:08 +08:00
|
|
|
dropdownSelectedClass = 'ant-table-filter-selected';
|
|
|
|
}
|
|
|
|
filterDropdown = <Dropdown trigger="click"
|
|
|
|
closeOnSelect={false}
|
|
|
|
overlay={menus}>
|
|
|
|
<i title="筛选" className={'anticon anticon-bars ' + dropdownSelectedClass}></i>
|
2015-07-13 16:22:02 +08:00
|
|
|
</Dropdown>;
|
|
|
|
}
|
|
|
|
if (column.sorter) {
|
2015-07-15 11:43:00 +08:00
|
|
|
let isSortColumn = (this.state.sortColumn === column);
|
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-13 16:22:02 +08:00
|
|
|
title="升序排序"
|
|
|
|
onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
|
|
|
|
<i className="anticon anticon-caret-up"></i>
|
|
|
|
</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-13 16:22:02 +08:00
|
|
|
title="降序排序"
|
|
|
|
onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
|
|
|
|
<i className="anticon anticon-caret-down"></i>
|
|
|
|
</span>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
column.title = [
|
|
|
|
column.originTitle,
|
|
|
|
sortButton,
|
|
|
|
filterDropdown
|
|
|
|
];
|
2015-07-13 17:40:17 +08:00
|
|
|
return column;
|
2015-07-13 16:22:02 +08:00
|
|
|
});
|
|
|
|
},
|
2015-07-14 15:35:17 +08:00
|
|
|
renderPagination() {
|
|
|
|
// 强制不需要分页
|
2015-07-27 19:58:51 +08:00
|
|
|
if (this.state.noPagination) {
|
2015-07-14 15:35:17 +08:00
|
|
|
return '';
|
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';
|
|
|
|
}
|
|
|
|
return <Pagination className={classString}
|
2015-07-14 15:35:17 +08:00
|
|
|
onChange={this.handlePageChange}
|
|
|
|
{...this.state.pagination} />;
|
2015-07-10 17:47:53 +08:00
|
|
|
},
|
2015-07-14 18:29:43 +08:00
|
|
|
prepareParamsArguments() {
|
|
|
|
// 准备筛选、排序、分页的参数
|
|
|
|
let pagination;
|
|
|
|
let filters = {};
|
2015-07-15 14:10:01 +08:00
|
|
|
let sorter = {};
|
2015-07-14 18:29:43 +08:00
|
|
|
pagination = this.state.pagination;
|
|
|
|
this.props.columns.forEach(function(column) {
|
|
|
|
if (column.dataIndex && column.selectedFilters &&
|
|
|
|
column.selectedFilters.length > 0) {
|
|
|
|
filters[column.dataIndex] = column.selectedFilters;
|
|
|
|
}
|
|
|
|
});
|
2015-07-15 14:10:01 +08:00
|
|
|
if (this.state.sortColumn && this.state.sortOrder &&
|
|
|
|
this.state.sortColumn.dataIndex) {
|
|
|
|
sorter.field = this.state.sortColumn.dataIndex;
|
|
|
|
sorter.order = this.state.sortOrder;
|
|
|
|
}
|
|
|
|
return [pagination, filters, sorter];
|
2015-07-14 18:29:43 +08:00
|
|
|
},
|
2015-07-27 15:38:06 +08:00
|
|
|
fetch() {
|
2015-07-14 15:35:17 +08:00
|
|
|
if (this.mode === 'remote') {
|
2015-07-16 23:16:20 +08:00
|
|
|
// remote 模式使用 this.dataSource
|
|
|
|
let dataSource = this.dataSource;
|
2015-07-12 17:10:06 +08:00
|
|
|
this.setState({
|
|
|
|
loading: true
|
|
|
|
});
|
|
|
|
jQuery.ajax({
|
2015-07-14 15:35:17 +08:00
|
|
|
url: dataSource.url,
|
2015-07-14 20:04:14 +08:00
|
|
|
data: dataSource.getParams.apply(this, this.prepareParamsArguments()) || {},
|
|
|
|
headers: dataSource.headers,
|
|
|
|
dataType: '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-14 17:58:00 +08:00
|
|
|
this.state.pagination,
|
|
|
|
dataSource.getPagination.call(this, result)
|
|
|
|
);
|
2015-07-12 17:10:06 +08:00
|
|
|
this.setState({
|
2015-07-14 15:35:17 +08:00
|
|
|
data: dataSource.resolve.call(this, result),
|
2015-07-14 17:58:00 +08:00
|
|
|
pagination: pagination,
|
|
|
|
loading: false
|
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({
|
|
|
|
loading: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-14 15:35:17 +08:00
|
|
|
} else {
|
2015-07-16 23:16:20 +08:00
|
|
|
let data = this.props.dataSource;
|
2015-07-21 16:47:52 +08:00
|
|
|
let current, pageSize;
|
|
|
|
// 如果没有分页的话,默认全部展示
|
2015-07-27 19:58:51 +08:00
|
|
|
if (this.state.noPagination) {
|
2015-07-27 15:38:06 +08:00
|
|
|
pageSize = Number.MAX_VALUE;
|
2015-07-21 16:47:52 +08:00
|
|
|
current = 1;
|
|
|
|
} else {
|
|
|
|
pageSize = this.state.pagination.pageSize;
|
|
|
|
current = this.state.pagination.current;
|
|
|
|
}
|
2015-07-16 18:26:17 +08:00
|
|
|
// 排序
|
|
|
|
if (this.state.sortOrder && this.state.sorter) {
|
|
|
|
data = data.sort(this.state.sorter);
|
|
|
|
} else {
|
|
|
|
data = this.originDataSource.slice();
|
|
|
|
}
|
|
|
|
// 筛选
|
|
|
|
if (this.state.filterFns) {
|
|
|
|
this.state.filterFns.forEach(function(filterFn) {
|
|
|
|
if (typeof filterFn === 'function') {
|
|
|
|
data = data.filter(filterFn);
|
2015-07-14 17:58:00 +08:00
|
|
|
}
|
2015-07-16 18:26:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// 分页
|
2015-07-29 13:49:44 +08:00
|
|
|
// ---
|
|
|
|
// 当数据量少于每页数量时,直接设置数据
|
|
|
|
// 否则进行读取分页数据
|
|
|
|
if (data.length > pageSize || pageSize === Number.MAX_VALUE) {
|
|
|
|
data = data.filter(function(item, i) {
|
|
|
|
if (i >= (current - 1) * pageSize &&
|
|
|
|
i < current * pageSize) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-16 18:26:17 +08:00
|
|
|
// 完成数据
|
|
|
|
this.setState({
|
|
|
|
data: data
|
2015-07-14 17:58:00 +08:00
|
|
|
});
|
2015-07-12 17:10:06 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
componentDidMount() {
|
2015-07-14 17:58:00 +08:00
|
|
|
this.handlePageChange();
|
2015-07-12 17:10:06 +08:00
|
|
|
},
|
2015-07-09 11:41:36 +08:00
|
|
|
render() {
|
2015-07-14 15:35:17 +08:00
|
|
|
this.props.columns = this.renderRowSelection();
|
|
|
|
|
2015-07-12 18:14:17 +08:00
|
|
|
var classString = '';
|
2015-07-14 21:06:39 +08:00
|
|
|
if (this.state.loading) {
|
2015-07-12 18:14:17 +08:00
|
|
|
classString += ' ant-table-loading';
|
|
|
|
}
|
|
|
|
if (this.props.size === 'small') {
|
|
|
|
classString += ' ant-table-small';
|
|
|
|
}
|
2015-07-14 15:35:17 +08:00
|
|
|
|
|
|
|
return <div className="clearfix">
|
|
|
|
<Table data={this.state.data}
|
|
|
|
columns={this.renderColumnsDropdown()}
|
2015-07-12 18:14:17 +08:00
|
|
|
className={classString}
|
2015-07-14 15:35:17 +08:00
|
|
|
{...this.props} />
|
|
|
|
{this.renderPagination()}
|
|
|
|
</div>;
|
2015-07-09 11:41:36 +08:00
|
|
|
}
|
|
|
|
});
|