sort by click column area

This commit is contained in:
afc163 2018-09-17 17:57:01 +08:00 committed by 偏右
parent 6f67c435c2
commit 27e2654f0f
5 changed files with 26 additions and 23 deletions

View File

@ -16,6 +16,7 @@ export default {
emptyText: 'No data',
selectAll: 'Select current page',
selectInvert: 'Invert current page',
sortTitle: 'Sort',
},
Modal: {
okText: 'OK',

View File

@ -16,6 +16,7 @@ export default {
emptyText: '暂无数据',
selectAll: '全选当页',
selectInvert: '反选当页',
sortTitle: '排序',
},
Modal: {
okText: '确定',

View File

@ -355,24 +355,22 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
};
}
toggleSortOrder(order: 'ascend'|'descend', column: ColumnProps<T>) {
let { sortColumn, sortOrder } = this.state;
toggleSortOrder(column: ColumnProps<T>) {
const { sortOrder } = this.state;
// 只同时允许一列进行排序,否则会导致排序顺序的逻辑问题
let isSortColumn = this.isSortColumn(column);
if (!isSortColumn) { // 当前列未排序
sortOrder = order;
sortColumn = column;
} else { // 当前列已排序
if (sortOrder === order) { // 切换为未排序状态
sortOrder = undefined;
sortColumn = null;
} else { // 切换为排序状态
sortOrder = order;
}
let newSortOrder: 'descend' | 'ascend' | undefined;
// 切换排序状态,按照降序/升序/不排序的顺序
if (!sortOrder) {
newSortOrder = 'descend';
} else if (sortOrder === 'descend') {
newSortOrder = 'ascend';
} else {
newSortOrder = undefined;
}
const newState = {
sortOrder,
sortColumn,
sortOrder: newSortOrder,
sortColumn: newSortOrder ? column : null,
};
// Controlled
@ -380,7 +378,7 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
this.setState(newState);
}
const onChange = this.props.onChange;
const { onChange } = this.props;
if (onChange) {
onChange.apply(null, this.prepareParamsArguments({
...this.state,
@ -791,13 +789,11 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
<div className={`${prefixCls}-column-sorter`} key="sorter">
<Icon
className={`${prefixCls}-column-sorter-up ${isAscend ? 'on' : 'off'}`}
title="↑"
type="caret-up"
theme="filled"
/>
<Icon
className={`${prefixCls}-column-sorter-down ${isDescend ? 'on' : 'off'}`}
title="↓"
type="caret-down"
theme="filled"
/>
@ -824,11 +820,12 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
const headerCellProps = column.onHeaderCell ? column.onHeaderCell(columnProps) : {};
return {
onClick: (e: MouseEvent) => {
this.handleColumnHeaderClick(e);
this.toggleSortOrder(column);
if (headerCellProps && headerCellProps.onClick) {
headerCellProps.onClick(e);
}
},
title: locale.sortTitle,
...headerCellProps,
};
},
@ -836,10 +833,6 @@ export default class Table<T> extends React.Component<TableProps<T>, TableState<
});
}
handleColumnHeaderClick = (e: MouseEvent) => {
console.log(e);
}
handleShowSizeChange = (current: number, pageSize: number) => {
const pagination = this.state.pagination;
pagination.onShowSizeChange!(current, pageSize);

View File

@ -57,6 +57,7 @@ export interface TableLocale {
emptyText?: React.ReactNode | (() => React.ReactNode);
selectAll?: React.ReactNode;
selectInvert?: React.ReactNode;
sortTitle?: string;
}
export type RowSelectionType = 'checkbox' | 'radio';

View File

@ -84,6 +84,9 @@
height: 4px;
transition: all .3s;
display: block;
&.on {
color: @primary-color;
}
}
&-down {
@ -128,6 +131,10 @@
&.@{table-prefix-cls}-column-has-filters .@{table-prefix-cls}-column-sorter {
right: 28px + 8px;
}
&.@{table-prefix-cls}-column-has-sorters {
user-select: none;
}
}
&-thead > tr:first-child > th {