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.

342 lines
10 KiB

10 years ago
import React from 'react';
import jQuery from 'jquery';
10 years ago
import Table from 'rc-table';
import Dropdown from '../dropdown';
import Checkbox from '../checkbox';
import FilterMenu from './filterMenu';
import Pagination from '../pagination';
import objectAssign from 'object-assign';
10 years ago
export default React.createClass({
10 years ago
getInitialState() {
// 支持两种模式
if (Array.isArray(this.props.dataSource)) {
this.mode = 'local';
// 保留原来的数据
this.originDataSource = this.props.dataSource.slice(0);
} else {
this.mode = 'remote';
this.dataSource = objectAssign({
resolve: function(data) {
return data || [];
},
getParams: function() {},
getPagination: function() {}
}, this.props.dataSource);
}
let noPagination = (this.props.pagination === false);
let pagination = objectAssign({
pageSize: 10,
total: this.props.dataSource.length
}, this.props.pagination);
10 years ago
return {
selectedRowKeys: [],
loading: false,
pagination: pagination,
noPagination: noPagination,
10 years ago
data: []
10 years ago
};
},
10 years ago
getDefaultProps() {
return {
prefixCls: 'ant-table',
10 years ago
useFixedHeader: false,
10 years ago
rowSelection: null,
size: 'normal'
10 years ago
};
},
toggleSortOrder(order, column) {
let sortColumn = this.state.sortColumn;
let sortOrder = this.state.sortOrder;
9 years ago
let sorter;
// 同时允许一列进行排序,否则会导致排序顺序的逻辑问题
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';
}
}
if (this.mode === 'local') {
9 years ago
sorter = function() {
let result = column.sorter.apply(this, arguments);
if (sortOrder === 'ascend') {
return result;
} else if (sortOrder === 'descend') {
return -result;
}
};
}
10 years ago
this.setState({
sortOrder: sortOrder,
9 years ago
sortColumn: sortColumn,
sorter: sorter
10 years ago
}, this.fetch);
},
10 years ago
handleFilter(column) {
9 years ago
let columnIndex = this.props.columns.indexOf(column);
let filterFns = [];
if (this.mode === 'local') {
9 years ago
filterFns[columnIndex] = function(record) {
if (column.selectedFilters.length === 0) {
return true;
}
return column.selectedFilters.some(function(value) {
return column.onFilter.call(this, value, record);
});
9 years ago
};
}
9 years ago
this.setState({
filterFns: filterFns
}, this.fetch);
10 years ago
},
handleSelect(rowIndex, e) {
let checked = e.target.checked;
if (checked) {
this.state.selectedRowKeys.push(rowIndex);
} else {
this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i) {
return rowIndex !== i;
});
}
this.setState({
selectedRowKeys: this.state.selectedRowKeys
});
if (this.props.rowSelection.onSelect) {
10 years ago
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);
}
},
handleSelectAllRow(e) {
let checked = e.target.checked;
let selectedRowKeys = checked ? this.state.data.map(function(item, i) {
return i + 1;
10 years ago
}) : [];
this.setState({
selectedRowKeys: selectedRowKeys
});
if (this.props.rowSelection.onSelectAll) {
10 years ago
let selectedRows = this.state.data.filter((row, i) => {
return selectedRowKeys.indexOf(i + 1) >= 0;
});
this.props.rowSelection.onSelectAll(checked, selectedRows);
10 years ago
}
},
handlePageChange(current = 1) {
let pagination = this.state.pagination || {};
pagination.current = current;
10 years ago
this.setState({
pagination: pagination
}, this.fetch);
},
renderSelectionCheckBox(value, record, index) {
let rowIndex = index + 1; // 从 1 开始
let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
return <Checkbox checked={checked} onChange={this.handleSelect.bind(this, rowIndex)} />;
},
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);
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;
},
renderColumnsDropdown() {
return this.props.columns.map((column) => {
if (!column.originTitle) {
column.originTitle = column.title;
}
let filterDropdown, menus, sortButton;
if (column.filters && column.filters.length > 0) {
column.selectedFilters = column.selectedFilters || [];
menus = <FilterMenu column={column} confirmFilter={this.handleFilter.bind(this, column)} />;
10 years ago
let dropdownSelectedClass = '';
if (column.selectedFilters && column.selectedFilters.length > 0) {
10 years ago
dropdownSelectedClass = 'ant-table-filter-selected';
}
filterDropdown = <Dropdown trigger="click"
closeOnSelect={false}
overlay={menus}>
<i title="筛选" className={'anticon anticon-bars ' + dropdownSelectedClass}></i>
</Dropdown>;
}
if (column.sorter) {
let isSortColumn = (this.state.sortColumn === column);
sortButton = <div className="ant-table-column-sorter">
<span className={'ant-table-column-sorter-up ' +
((isSortColumn && this.state.sortOrder === 'ascend') ? 'on' : 'off')}
title="升序排序"
onClick={this.toggleSortOrder.bind(this, 'ascend', column)}>
<i className="anticon anticon-caret-up"></i>
</span>
<span className={'ant-table-column-sorter-down ' +
((isSortColumn && this.state.sortOrder === 'descend') ? 'on' : 'off')}
title="降序排序"
onClick={this.toggleSortOrder.bind(this, 'descend', column)}>
<i className="anticon anticon-caret-down"></i>
</span>
</div>;
}
column.title = [
column.originTitle,
sortButton,
filterDropdown
];
return column;
});
},
renderPagination() {
// 强制不需要分页
if (this.state.noPagination) {
return '';
10 years ago
}
let classString = 'ant-table-pagination';
if (this.props.size === 'small') {
classString += ' mini';
}
return <Pagination className={classString}
onChange={this.handlePageChange}
{...this.state.pagination} />;
10 years ago
},
prepareParamsArguments() {
// 准备筛选、排序、分页的参数
let pagination;
let filters = {};
10 years ago
let sorter = {};
pagination = this.state.pagination;
this.props.columns.forEach(function(column) {
if (column.dataIndex && column.selectedFilters &&
column.selectedFilters.length > 0) {
filters[column.dataIndex] = column.selectedFilters;
}
});
10 years ago
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];
},
fetch() {
if (this.mode === 'remote') {
// remote 模式使用 this.dataSource
let dataSource = this.dataSource;
this.setState({
loading: true
});
jQuery.ajax({
url: dataSource.url,
data: dataSource.getParams.apply(this, this.prepareParamsArguments()) || {},
headers: dataSource.headers,
dataType: 'json',
success: (result) => {
if (this.isMounted()) {
let pagination = objectAssign(
10 years ago
this.state.pagination,
dataSource.getPagination.call(this, result)
);
this.setState({
data: dataSource.resolve.call(this, result),
10 years ago
pagination: pagination,
loading: false
});
}
},
10 years ago
error: () => {
this.setState({
loading: false
});
}
});
} else {
let data = this.props.dataSource;
let current, pageSize;
// 如果没有分页的话,默认全部展示
if (this.state.noPagination) {
pageSize = Number.MAX_VALUE;
current = 1;
} else {
pageSize = this.state.pagination.pageSize;
current = this.state.pagination.current;
}
9 years ago
// 排序
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);
10 years ago
}
9 years ago
});
}
// 分页
data = data.filter(function(item, i) {
if (i >= (current - 1) * pageSize &&
i < current * pageSize) {
return item;
}
});
// 完成数据
this.setState({
data: data
10 years ago
});
}
},
componentDidMount() {
10 years ago
this.handlePageChange();
},
10 years ago
render() {
this.props.columns = this.renderRowSelection();
10 years ago
var classString = '';
if (this.state.loading) {
10 years ago
classString += ' ant-table-loading';
}
if (this.props.size === 'small') {
classString += ' ant-table-small';
}
return <div className="clearfix">
<Table data={this.state.data}
columns={this.renderColumnsDropdown()}
10 years ago
className={classString}
{...this.props} />
{this.renderPagination()}
</div>;
10 years ago
}
});