31741db62fc4287b684d90ca742700c283505aa603806fde37ad535934fa3cc8b2f3a0364e40074e12f7a9cb511b63883cb624a953a76cc804e3a5eebd1372 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { toggleRowStatus, getKeysMap, getRowIdentity } from '../util';
  2. export default {
  3. data() {
  4. return {
  5. states: {
  6. defaultExpandAll: false,
  7. expandRows: []
  8. }
  9. };
  10. },
  11. methods: {
  12. updateExpandRows() {
  13. const { data = [], rowKey, defaultExpandAll, expandRows } = this.states;
  14. if (defaultExpandAll) {
  15. this.states.expandRows = data.slice();
  16. } else if (rowKey) {
  17. // TODO:这里的代码可以优化
  18. const expandRowsMap = getKeysMap(expandRows, rowKey);
  19. this.states.expandRows = data.reduce((prev, row) => {
  20. const rowId = getRowIdentity(row, rowKey);
  21. const rowInfo = expandRowsMap[rowId];
  22. if (rowInfo) {
  23. prev.push(row);
  24. }
  25. return prev;
  26. }, []);
  27. } else {
  28. this.states.expandRows = [];
  29. }
  30. },
  31. toggleRowExpansion(row, expanded) {
  32. const changed = toggleRowStatus(this.states.expandRows, row, expanded);
  33. if (changed) {
  34. this.table.$emit('expand-change', row, this.states.expandRows.slice());
  35. this.scheduleLayout();
  36. }
  37. },
  38. setExpandRowKeys(rowKeys) {
  39. this.assertRowKey();
  40. // TODO:这里的代码可以优化
  41. const { data, rowKey } = this.states;
  42. const keysMap = getKeysMap(data, rowKey);
  43. this.states.expandRows = rowKeys.reduce((prev, cur) => {
  44. const info = keysMap[cur];
  45. if (info) {
  46. prev.push(info.row);
  47. }
  48. return prev;
  49. }, []);
  50. },
  51. isRowExpanded(row) {
  52. const { expandRows = [], rowKey } = this.states;
  53. if (rowKey) {
  54. const expandMap = getKeysMap(expandRows, rowKey);
  55. return !!expandMap[getRowIdentity(row, rowKey)];
  56. }
  57. return expandRows.indexOf(row) !== -1;
  58. }
  59. }
  60. };