Browse Source

Merge pull request #2309 from njleonzhang/fix_sortevent_bug

fix bug: default-sort-prop is not defined but trigger sort-change event
baiyaaaaa 8 years ago
parent
commit
5246b170f4
2 changed files with 19 additions and 13 deletions
  1. 18 12
      packages/table/src/table-header.js
  2. 1 1
      test/unit/specs/table.spec.js

+ 18 - 12
packages/table/src/table-header.js

@@ -190,20 +190,26 @@ export default {
   },
 
   mounted() {
-    const states = this.store.states;
-    states.sortProp = this.defaultSortProp;
-    states.sortOrder = this.defaultSortOrder;
-
-    this.$nextTick(_ => {
-      for (let i = 0, length = this.columns.length; i < length; i++) {
-        if (this.columns[i].property === this.defaultSortProp) {
-          this.columns[i].order = this.defaultSortOrder;
-          break;
+    if (this.defaultSortProp) {
+      const states = this.store.states;
+      states.sortProp = this.defaultSortProp;
+      states.sortOrder = this.defaultSortOrder;
+
+      this.$nextTick(_ => {
+        for (let i = 0, length = this.columns.length; i < length; i++) {
+          let column = this.columns[i];
+          if (column.property === this.defaultSortProp) {
+            column.order = this.defaultSortOrder;
+            states.sortingColumn = column;
+            break;
+          }
         }
-      }
 
-      this.store.commit('changeSortCondition');
-    });
+        if (states.sortingColumn) {
+          this.store.commit('changeSortCondition');
+        }
+      });
+    }
   },
 
   beforeDestroy() {

+ 1 - 1
test/unit/specs/table.spec.js

@@ -1127,7 +1127,7 @@ describe('Table', () => {
           }
         }, '@sort-change="sortChange"');
         setTimeout(_ => {
-          const elm = vm.$el.querySelector('.caret-wrapper > .ascending');
+          const elm = vm.$el.querySelector('.caret-wrapper');
 
           elm.click();
           setTimeout(_ => {