Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table: retain currently selected row when sorting the table. #11348

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/table/src/table-store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from 'vue';
import debounce from 'throttle-debounce/debounce';
import merge from 'element-ui/src/utils/merge';
import { hasClass, addClass, removeClass } from 'element-ui/src/utils/dom';
import { orderBy, getColumnById, getRowIdentity } from './util';

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

const el = this.table.$el;
if (el) {
const data = states.data;
const tr = el.querySelector('tbody').children;
const rows = [].filter.call(tr, row => hasClass(row, 'el-table__row'));
const row = rows[data.indexOf(states.currentRow)];

[].forEach.call(rows, row => removeClass(row, 'current-row'));
addClass(row, 'current-row');
}

if (!options || !options.silent) {
this.table.$emit('sort-change', {
column: this.states.sortingColumn,
Expand Down
20 changes: 15 additions & 5 deletions test/unit/specs/table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ describe('Table', () => {
<el-table-column prop="name" label="片名" />
<el-table-column prop="release" label="发行日期" />
<el-table-column prop="director" label="导演" />
<el-table-column prop="runtime" label="时长(分)" />
<el-table-column prop="runtime" label="时长(分)" sortable />
</el-table>
`,

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

triggerEvent(rows[2], 'click', true, false);
triggerEvent(rows[1], 'click', true, false);
setTimeout(_ => {
expect(tr.classList.contains('current-row')).to.be.false;
expect(rows[2].classList.contains('current-row')).to.be.true;
destroyVM(vm);
done();
expect(rows[1].classList.contains('current-row')).to.be.true;

const ths = vm.$el.querySelectorAll('.el-table__header-wrapper thead th');
triggerEvent(ths[3], 'click', true, false);

setTimeout(_ => {
const rows = vm.$el.querySelectorAll('.el-table__body-wrapper tbody tr');

expect(rows[1].classList.contains('current-row')).to.be.false;
expect(rows[3].classList.contains('current-row')).to.be.true;
destroyVM(vm);
done();
}, DELAY);
}, DELAY);
}, DELAY);
}, DELAY);
Expand Down