From 191235fcdc606debd6447a2f322bd39ade47db0f Mon Sep 17 00:00:00 2001 From: afc163 Date: Wed, 15 Jul 2015 11:43:00 +0800 Subject: [PATCH] fix sort table logic, only allow one column to be sorted at a time --- components/table/index.jsx | 41 ++++++++++++++++++++++++++++--------- style/components/table.less | 4 ++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/components/table/index.jsx b/components/table/index.jsx index a34f16e879..4923af12da 100644 --- a/components/table/index.jsx +++ b/components/table/index.jsx @@ -38,7 +38,9 @@ let AntTable = React.createClass({ selectedRowKeys: [], loading: false, pagination: pagination, - data: [] + data: [], + sortColumn: null, + sortOrder: null }; }, getDefaultProps() { @@ -56,21 +58,39 @@ let AntTable = React.createClass({ return menuItems; }, toggleSortOrder(order, column) { - if (column.sortOrder === order) { - column.sortOrder = ''; - } else { - column.sortOrder = order; + 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'; + } + } + this.setState({ + sortOrder: sortOrder, + sortColumn: sortColumn + }); if (this.mode === 'local') { let sorter = function() { let result = column.sorter.apply(this, arguments); - if (column.sortOrder === 'ascend') { + if (sortOrder === 'ascend') { return result; - } else if (column.sortOrder === 'descend') { + } else if (sortOrder === 'descend') { return -result; } }; - if (column.sortOrder) { + if (sortOrder) { this.props.dataSource = this.props.dataSource.sort(sorter); } else { this.props.dataSource = this.originDataSource.slice(); @@ -215,15 +235,16 @@ let AntTable = React.createClass({ ; } if (column.sorter) { + let isSortColumn = (this.state.sortColumn === column); sortButton =
diff --git a/style/components/table.less b/style/components/table.less index 91ab339e92..0e8dd1ece1 100644 --- a/style/components/table.less +++ b/style/components/table.less @@ -86,6 +86,10 @@ background: #fafafa; } + th.@{tablePrefixClass}-column-sort { + background: #EAEAEA; + } + th, td { padding: 16px 8px; }