debb90c0bacac3456ff64fdace2e1fa6933485653adbf4a0acb54ffd3b0b266c6140a41734950cb3b86ee97b7ed0131495a70f06fe28f0d7c7b43e132199df 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import Vue from 'vue';
  2. import Watcher from './watcher';
  3. import { arrayFind } from 'element-ui/src/utils/util';
  4. Watcher.prototype.mutations = {
  5. setData(states, data) {
  6. const dataInstanceChanged = states._data !== data;
  7. states._data = data;
  8. this.execQuery();
  9. // 数据变化,更新部分数据。
  10. // 没有使用 computed,而是手动更新部分数据 https://github.com/vuejs/vue/issues/6660#issuecomment-331417140
  11. this.updateCurrentRowData();
  12. this.updateExpandRows();
  13. if (states.reserveSelection) {
  14. this.assertRowKey();
  15. this.updateSelectionByRowKey();
  16. } else {
  17. if (dataInstanceChanged) {
  18. this.clearSelection();
  19. } else {
  20. this.cleanSelection();
  21. }
  22. }
  23. this.updateAllSelected();
  24. this.updateTableScrollY();
  25. },
  26. insertColumn(states, column, index, parent) {
  27. let array = states._columns;
  28. if (parent) {
  29. array = parent.children;
  30. if (!array) array = parent.children = [];
  31. }
  32. if (typeof index !== 'undefined') {
  33. array.splice(index, 0, column);
  34. } else {
  35. array.push(column);
  36. }
  37. if (column.type === 'selection') {
  38. states.selectable = column.selectable;
  39. states.reserveSelection = column.reserveSelection;
  40. }
  41. if (this.table.$ready) {
  42. this.updateColumns(); // hack for dynamics insert column
  43. this.scheduleLayout();
  44. }
  45. },
  46. removeColumn(states, column, parent) {
  47. let array = states._columns;
  48. if (parent) {
  49. array = parent.children;
  50. if (!array) array = parent.children = [];
  51. }
  52. if (array) {
  53. array.splice(array.indexOf(column), 1);
  54. }
  55. if (this.table.$ready) {
  56. this.updateColumns(); // hack for dynamics remove column
  57. this.scheduleLayout();
  58. }
  59. },
  60. sort(states, options) {
  61. const { prop, order, init } = options;
  62. if (prop) {
  63. const column = arrayFind(states.columns, column => column.property === prop);
  64. if (column) {
  65. column.order = order;
  66. this.updateSort(column, prop, order);
  67. this.commit('changeSortCondition', { init });
  68. }
  69. }
  70. },
  71. changeSortCondition(states, options) {
  72. // 修复 pr https://github.com/ElemeFE/element/pull/15012 导致的 bug
  73. const { sortingColumn: column, sortProp: prop, sortOrder: order } = states;
  74. if (order === null) {
  75. states.sortingColumn = null;
  76. states.sortProp = null;
  77. }
  78. const ingore = { filter: true };
  79. this.execQuery(ingore);
  80. if (!options || !(options.silent || options.init)) {
  81. this.table.$emit('sort-change', {
  82. column,
  83. prop,
  84. order
  85. });
  86. }
  87. this.updateTableScrollY();
  88. },
  89. filterChange(states, options) {
  90. let { column, values, silent } = options;
  91. const newFilters = this.updateFilters(column, values);
  92. this.execQuery();
  93. if (!silent) {
  94. this.table.$emit('filter-change', newFilters);
  95. }
  96. this.updateTableScrollY();
  97. },
  98. toggleAllSelection() {
  99. this.toggleAllSelection();
  100. },
  101. rowSelectedChanged(states, row) {
  102. this.toggleRowSelection(row);
  103. this.updateAllSelected();
  104. },
  105. setHoverRow(states, row) {
  106. states.hoverRow = row;
  107. },
  108. setCurrentRow(states, row) {
  109. this.updateCurrentRow(row);
  110. }
  111. };
  112. Watcher.prototype.commit = function(name, ...args) {
  113. const mutations = this.mutations;
  114. if (mutations[name]) {
  115. mutations[name].apply(this, [this.states].concat(args));
  116. } else {
  117. throw new Error(`Action not found: ${name}`);
  118. }
  119. };
  120. Watcher.prototype.updateTableScrollY = function() {
  121. Vue.nextTick(this.table.updateScrollY);
  122. };
  123. export default Watcher;