ant-design/components/table/index.jsx

362 lines
11 KiB
React
Raw Normal View History

2015-07-10 17:47:53 +08:00
'use strict';
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 Menu from 'rc-menu';
import Dropdown from '../dropdown';
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
let AntTable = React.createClass({
2015-07-10 17:47:53 +08:00
getInitialState() {
// 支持两种模式
if (Array.isArray(this.props.dataSource)) {
this.mode = 'local';
// 保留原来的数据
this.originDataSource = this.props.dataSource.slice(0);
} else {
this.mode = 'remote';
2015-07-14 20:04:14 +08:00
this.props.dataSource = objectAssign({
resolve: function(data) {
return data || [];
2015-07-14 20:04:14 +08:00
},
getParams: function() {},
getPagination: function() {}
}, this.props.dataSource);
}
var pagination;
if (this.props.pagination === false) {
pagination = false;
} else {
2015-07-14 20:04:14 +08:00
pagination = objectAssign({
pageSize: 10,
total: this.props.dataSource.length
}, this.props.pagination);
}
2015-07-10 17:47:53 +08:00
return {
2015-07-12 17:10:06 +08:00
selectedRowKeys: [],
loading: false,
pagination: pagination,
data: [],
sortColumn: null,
sortOrder: null
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-13 16:22:02 +08:00
renderMenus(items) {
let menuItems = items.map((item) => {
2015-07-13 18:42:08 +08:00
return <Menu.Item key={item.value}>{item.text}</Menu.Item>;
2015-07-13 16:22:02 +08:00
});
return menuItems;
},
toggleSortOrder(order, column) {
let sortColumn = this.state.sortColumn;
let sortOrder = this.state.sortOrder;
// 同时允许一列进行排序,否则会导致排序顺序的逻辑问题
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
}
this.setState({
sortOrder: sortOrder,
sortColumn: sortColumn
});
if (this.mode === 'local') {
let sorter = function() {
let result = column.sorter.apply(this, arguments);
if (sortOrder === 'ascend') {
return result;
} else if (sortOrder === 'descend') {
return -result;
}
};
if (sortOrder) {
this.props.dataSource = this.props.dataSource.sort(sorter);
} else {
this.props.dataSource = this.originDataSource.slice();
}
2015-07-13 16:22:02 +08:00
}
this.fetch();
2015-07-13 16:22:02 +08:00
},
2015-07-13 18:42:08 +08:00
handleFilter(column) {
if (this.mode === 'local') {
this.props.dataSource = this.originDataSource.slice().filter(function(record) {
if (column.selectedFilters.length === 0) {
return true;
}
return column.selectedFilters.some(function(value) {
return column.onFilter.call(this, value, record);
});
});
}
this.fetch();
2015-07-13 18:42:08 +08:00
},
handleSelectFilter(column, selected) {
column.selectedFilters.push(selected);
2015-07-13 17:40:17 +08:00
},
2015-07-13 18:42:08 +08:00
handleDeselectFilter(column, key) {
var index = column.selectedFilters.indexOf(key);
2015-07-13 18:42:08 +08:00
if (index !== -1) {
column.selectedFilters.splice(index, 1);
}
},
handleClearFilters(column) {
column.selectedFilters = [];
this.fetch();
},
handleSelect(e) {
let checked = e.currentTarget.checked;
let currentRowIndex = e.currentTarget.parentElement.parentElement.rowIndex;
let selectedRow = this.state.data[currentRowIndex - 1];
if (checked) {
this.state.selectedRowKeys.push(currentRowIndex);
} else {
2015-07-15 11:12:17 +08:00
this.state.selectedRowKeys = this.state.selectedRowKeys.filter(function(i) {
return currentRowIndex !== i;
});
}
this.setState({
selectedRowKeys: this.state.selectedRowKeys
});
if (this.props.rowSelection.onSelect) {
this.props.rowSelection.onSelect(selectedRow, checked);
}
},
handleSelectAllRow(e) {
let checked = e.currentTarget.checked;
this.setState({
selectedRowKeys: checked ? this.state.data.map(function(item, i) {
return i + 1;
}) : []
});
if (this.props.rowSelection.onSelectAll) {
this.props.rowSelection.onSelectAll(checked);
2015-07-13 18:42:08 +08:00
}
2015-07-13 17:40:17 +08:00
},
handlePageChange: function(current) {
2015-07-14 17:58:00 +08:00
this.state.pagination.current = current || 1;
this.fetch();
},
renderSelectionCheckBox(value, record, index) {
let checked = this.state.selectedRowKeys.indexOf(index + 1) >= 0;
let checkbox = <input type="checkbox" checked={checked} onChange={this.handleSelect} />;
return checkbox;
},
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 = <input type="checkbox" checked={checked} onChange={this.handleSelectAllRow} />;
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'
};
if (columns[0] &&
columns[0].key === 'selection-column') {
columns[0] = selectionColumn;
} else {
columns.unshift(selectionColumn);
}
2015-07-15 11:12:17 +08:00
// 加上选中行的样式
this.props.rowClassName = (record, i) => {
return this.state.selectedRowKeys.indexOf(i + 1) >= 0 ?
'ant-table-row-selected' : '';
};
}
return columns;
},
2015-07-13 16:22:02 +08:00
renderColumnsDropdown() {
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;
if (column.filters && column.filters.length > 0) {
column.selectedFilters = column.selectedFilters || [];
2015-07-13 18:42:08 +08:00
menus = <Menu multiple={true}
className="ant-table-filter-dropdown"
onSelect={this.handleSelectFilter.bind(this, column)}
onDeselect={this.handleDeselectFilter.bind(this, column)}
selectedKeys={column.selectedFilters}>
{this.renderMenus(column.filters)}
<Menu.Divider />
2015-07-13 18:42:08 +08:00
<Menu.Item disabled>
<a className="ant-table-filter-dropdown-link confirm"
style={{
2015-07-13 18:42:08 +08:00
cursor: 'pointer',
pointerEvents: 'visible'
}}
onClick={this.handleFilter.bind(this, column)}>
确定
</a>
<a className="ant-table-filter-dropdown-link clear"
style={{
cursor: 'pointer',
pointerEvents: 'visible'
}}
onClick={this.handleClearFilters.bind(this, column)}>
清空
</a>
2015-07-13 18:42:08 +08:00
</Menu.Item>
2015-07-13 16:22:02 +08:00
</Menu>;
2015-07-13 18:42:08 +08:00
let dropdownSelectedClass = '';
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) {
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 ' +
((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 ' +
((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
});
},
renderPagination() {
// 强制不需要分页
if (this.state.pagination === false) {
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}
onChange={this.handlePageChange}
{...this.state.pagination} />;
2015-07-10 17:47:53 +08:00
},
prepareParamsArguments() {
// 准备筛选、排序、分页的参数
let pagination;
let filters = {};
let sorters = {};
pagination = this.state.pagination;
this.props.columns.forEach(function(column) {
if (column.dataIndex && column.selectedFilters &&
column.selectedFilters.length > 0) {
filters[column.dataIndex] = column.selectedFilters;
}
if (column.dataIndex && column.sorter &&
column.sortOrder) {
sorters[column.dataIndex] = column.sortOrder;
}
});
return [pagination, filters, sorters];
},
fetch: function() {
let dataSource = this.props.dataSource;
if (this.mode === 'remote') {
2015-07-12 17:10:06 +08:00
this.setState({
loading: true
});
jQuery.ajax({
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({
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
});
}
});
} else {
2015-07-14 17:58:00 +08:00
let pageSize = this.state.pagination.pageSize;
let current = this.state.pagination.current;
this.setState({
data: this.props.dataSource.filter(function(item, i) {
if (i >= (current - 1) * pageSize &&
i < current * pageSize) {
return item;
}
}),
pagination: this.state.pagination
});
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() {
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';
}
return <div className="clearfix">
<Table data={this.state.data}
columns={this.renderColumnsDropdown()}
2015-07-12 18:14:17 +08:00
className={classString}
{...this.props} />
{this.renderPagination()}
</div>;
2015-07-09 11:41:36 +08:00
}
});
export default AntTable;