'use strict';
import React from 'react';
import jQuery from 'jquery';
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';
let AntTable = React.createClass({
getInitialState() {
// 支持两种模式
if (Array.isArray(this.props.dataSource)) {
this.mode = 'local';
// 保留原来的数据
this.originDataSource = this.props.dataSource.slice(0);
} else {
this.mode = 'remote';
this.props.dataSource = objectAssign({
resolve: function(data) {
return data || [];
},
getParams: function() {},
getPagination: function() {}
}, this.props.dataSource);
}
var pagination;
if (this.props.pagination === false) {
pagination = false;
} else {
pagination = objectAssign({
pageSize: 10,
total: this.props.dataSource.length
}, this.props.pagination);
}
return {
selectedRowKeys: [],
loading: false,
pagination: pagination,
data: []
};
},
getDefaultProps() {
return {
prefixCls: 'ant-table',
useFixedHeader: false,
rowSelection: null,
size: 'normal'
};
},
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';
}
}
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();
}
}
this.setState({
sortOrder: sortOrder,
sortColumn: sortColumn
}, this.fetch);
},
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();
},
handleSelect(rowIndex, checked) {
let selectedRow = this.state.data[rowIndex - 1];
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) {
this.props.rowSelection.onSelect(selectedRow, checked);
}
},
handleSelectAllRow(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);
}
},
handlePageChange: function(current) {
let pagination = this.state.pagination;
pagination.current = current || 1;
this.setState({
pagination: pagination
}, this.fetch);
},
renderSelectionCheckBox(value, record, index) {
let rowIndex = index + 1; // 从 1 开始
let checked = this.state.selectedRowKeys.indexOf(rowIndex) >= 0;
return