Skip to content

Commit b726aa0

Browse files
jikkaiLeopoldthecoder
authored andcommitted
Table: retain currently selected row when sorting the table. (#11348)
1 parent c128914 commit b726aa0

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

packages/table/src/table-store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from 'vue';
22
import debounce from 'throttle-debounce/debounce';
33
import merge from 'element-ui/src/utils/merge';
4+
import { hasClass, addClass, removeClass } from 'element-ui/src/utils/dom';
45
import { orderBy, getColumnById, getRowIdentity } from './util';
56

67
const sortData = (data, states) => {
@@ -192,6 +193,17 @@ TableStore.prototype.mutations = {
192193
changeSortCondition(states, options) {
193194
states.data = sortData((states.filteredData || states._data || []), states);
194195

196+
const el = this.table.$el;
197+
if (el) {
198+
const data = states.data;
199+
const tr = el.querySelector('tbody').children;
200+
const rows = [].filter.call(tr, row => hasClass(row, 'el-table__row'));
201+
const row = rows[data.indexOf(states.currentRow)];
202+
203+
[].forEach.call(rows, row => removeClass(row, 'current-row'));
204+
addClass(row, 'current-row');
205+
}
206+
195207
if (!options || !options.silent) {
196208
this.table.$emit('sort-change', {
197209
column: this.states.sortingColumn,

test/unit/specs/table.spec.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ describe('Table', () => {
17851785
<el-table-column prop="name" label="片名" />
17861786
<el-table-column prop="release" label="发行日期" />
17871787
<el-table-column prop="director" label="导演" />
1788-
<el-table-column prop="runtime" label="时长(分)" />
1788+
<el-table-column prop="runtime" label="时长(分)" sortable />
17891789
</el-table>
17901790
`,
17911791

@@ -1800,12 +1800,22 @@ describe('Table', () => {
18001800
expect(tr.classList.contains('current-row')).to.be.true;
18011801
const rows = vm.$el.querySelectorAll('.el-table__body-wrapper tbody tr');
18021802

1803-
triggerEvent(rows[2], 'click', true, false);
1803+
triggerEvent(rows[1], 'click', true, false);
18041804
setTimeout(_ => {
18051805
expect(tr.classList.contains('current-row')).to.be.false;
1806-
expect(rows[2].classList.contains('current-row')).to.be.true;
1807-
destroyVM(vm);
1808-
done();
1806+
expect(rows[1].classList.contains('current-row')).to.be.true;
1807+
1808+
const ths = vm.$el.querySelectorAll('.el-table__header-wrapper thead th');
1809+
triggerEvent(ths[3], 'click', true, false);
1810+
1811+
setTimeout(_ => {
1812+
const rows = vm.$el.querySelectorAll('.el-table__body-wrapper tbody tr');
1813+
1814+
expect(rows[1].classList.contains('current-row')).to.be.false;
1815+
expect(rows[3].classList.contains('current-row')).to.be.true;
1816+
destroyVM(vm);
1817+
done();
1818+
}, DELAY);
18091819
}, DELAY);
18101820
}, DELAY);
18111821
}, DELAY);

0 commit comments

Comments
 (0)