Skip to content

Commit db48ec3

Browse files
committed
fix: table trigger sort-change when mounted
1 parent 91297a9 commit db48ec3

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

packages/table/src/store/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ Watcher.prototype.mutations = {
6868
},
6969

7070
sort(states, options) {
71-
const { prop, order } = options;
71+
const { prop, order, init } = options;
7272
if (prop) {
7373
const column = arrayFind(states.columns, column => column.property === prop);
7474
if (column) {
7575
column.order = order;
7676
this.updateSort(column, prop, order);
77-
this.commit('changeSortCondition');
77+
this.commit('changeSortCondition', { init });
7878
}
7979
}
8080
},
@@ -89,7 +89,7 @@ Watcher.prototype.mutations = {
8989
const ingore = { filter: true };
9090
this.execQuery(ingore);
9191

92-
if (!options || !options.silent) {
92+
if (!options || !(options.silent || options.init)) {
9393
this.table.$emit('sort-change', {
9494
column,
9595
prop,

test/unit/specs/table.spec.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,38 @@ describe('Table', () => {
555555
done();
556556
}, DELAY);
557557
});
558+
559+
it('sort-change', async() => {
560+
const vm = createVue({
561+
template: `
562+
<el-table ref="table" :data="testData" :default-sort = "{prop: 'runtime', order: 'ascending'}">
563+
<el-table-column prop="name" />
564+
<el-table-column prop="release" />
565+
<el-table-column prop="director" />
566+
<el-table-column prop="runtime" sortable/>
567+
</el-table>
568+
`,
569+
570+
created() {
571+
this.testData = getTestData();
572+
},
573+
574+
data() {
575+
return { testData: this.testData };
576+
}
577+
});
578+
579+
const spy = sinon.spy();
580+
vm.$refs.table.$on('sort-change', spy);
581+
await waitImmediate();
582+
expect(spy.notCalled).to.be.true;// not emit when mounted
583+
584+
const elm = vm.$el.querySelector('.caret-wrapper');
585+
elm.click();
586+
await waitImmediate();
587+
expect(spy.calledOnce).to.be.true;
588+
destroyVM(vm);
589+
});
558590
});
559591

560592
describe('column attributes', () => {
@@ -1144,27 +1176,6 @@ describe('Table', () => {
11441176
}, DELAY);
11451177
}, DELAY);
11461178
});
1147-
1148-
it('sort-change', done => {
1149-
let result;
1150-
const vm = createTable('sortable="custom"', '', '', '', {
1151-
methods: {
1152-
sortChange(...args) {
1153-
result = args;
1154-
}
1155-
}
1156-
}, '@sort-change="sortChange"');
1157-
setTimeout(_ => {
1158-
const elm = vm.$el.querySelector('.caret-wrapper');
1159-
1160-
elm.click();
1161-
setTimeout(_ => {
1162-
expect(result).to.exist;
1163-
destroyVM(vm);
1164-
done();
1165-
}, DELAY);
1166-
}, DELAY);
1167-
});
11681179
});
11691180

11701181
describe('click sortable column', () => {

0 commit comments

Comments
 (0)