Blame view

src/components/table/table.vue 14.4 KB
2cb8a6d9   梁灏   commit Table comp...
1
  <template>
d3dfdb26   梁灏   update Table
2
      {{objData|json}}
e7e8c8ff   梁灏   update Table
3
4
      <div :class="classes" :style="styles">
          <div :class="[prefixCls + '-title']" v-if="showSlotHeader" v-el:title><slot name="header"></slot></div>
192e2cb8   梁灏   update Table
5
          <div :class="[prefixCls + '-header']" v-if="showHeader" v-el:header @mousewheel="handleMouseWheel">
7f34c510   梁灏   update Table
6
7
8
9
              <table-head
                  :prefix-cls="prefixCls"
                  :style="tableStyle"
                  :columns="cloneColumns"
d3dfdb26   梁灏   update Table
10
                  :obj-data="objData"></table-head>
744eb0af   梁灏   update Table comp...
11
          </div>
3ef4dfb9   梁灏   update Table
12
          <div :class="[prefixCls + '-body']" :style="bodyStyle" v-el:body @scroll="handleBodyScroll">
7f34c510   梁灏   update Table
13
14
15
16
17
              <table-body
                  v-ref:tbody
                  :prefix-cls="prefixCls"
                  :style="tableStyle"
                  :columns="cloneColumns"
741b987a   梁灏   update Table
18
                  :data="rebuildData"
d3dfdb26   梁灏   update Table
19
                  :obj-data="objData"></table-body>
2cb8a6d9   梁灏   commit Table comp...
20
          </div>
abdec99d   梁灏   update Table
21
          <div :class="[prefixCls + '-fixed']">
b8a43000   梁灏   update Table
22
23
24
25
26
27
              <div :class="[prefixCls + '-fixed-header']" v-if="showHeader">
                  <table-head
                      fixed
                      :prefix-cls="prefixCls"
                      :style="fixedTableStyle"
                      :columns="leftFixedColumns"
d3dfdb26   梁灏   update Table
28
                      :obj-data="objData"></table-head>
b8a43000   梁灏   update Table
29
30
31
32
33
34
35
              </div>
              <div :class="[prefixCls + '-fixed-body']" :style="fixedBodyStyle" v-el:fixed-body>
                  <table-body
                      fixed
                      :prefix-cls="prefixCls"
                      :style="fixedTableStyle"
                      :columns="leftFixedColumns"
741b987a   梁灏   update Table
36
                      :data="rebuildData"
d3dfdb26   梁灏   update Table
37
                      :obj-data="objData"></table-body>
b8a43000   梁灏   update Table
38
              </div>
abdec99d   梁灏   update Table
39
40
          </div>
          <div :class="[prefixCls + '-fixed-right']">
b8a43000   梁灏   update Table
41
42
43
44
45
46
              <div :class="[prefixCls + '-fixed-header']" v-if="showHeader">
                  <table-head
                      fixed
                      :prefix-cls="prefixCls"
                      :style="fixedRightTableStyle"
                      :columns="rightFixedColumns"
d3dfdb26   梁灏   update Table
47
                      :obj-data="objData"></table-head>
b8a43000   梁灏   update Table
48
49
50
51
52
53
54
              </div>
              <div :class="[prefixCls + '-fixed-body']" :style="fixedBodyStyle" v-el:fixed-right-body>
                  <table-body
                      fixed
                      :prefix-cls="prefixCls"
                      :style="fixedRightTableStyle"
                      :columns="rightFixedColumns"
741b987a   梁灏   update Table
55
                      :data="rebuildData"
d3dfdb26   梁灏   update Table
56
                      :obj-data="objData"></table-body>
b8a43000   梁灏   update Table
57
              </div>
abdec99d   梁灏   update Table
58
          </div>
e7e8c8ff   梁灏   update Table
59
          <div :class="[prefixCls + '-footer']" v-if="showSlotFooter" v-el:footer><slot name="footer"></slot></div>
2cb8a6d9   梁灏   commit Table comp...
60
61
62
      </div>
  </template>
  <script>
7f34c510   梁灏   update Table
63
64
      import tableHead from './table-head.vue';
      import tableBody from './table-body.vue';
0d136465   梁灏   update Table
65
      import { oneOf, getStyle, deepCopy } from '../../utils/assist';
2cb8a6d9   梁灏   commit Table comp...
66
67
68
      const prefixCls = 'ivu-table';
  
      export default {
7f34c510   梁灏   update Table
69
          components: { tableHead, tableBody },
2cb8a6d9   梁灏   commit Table comp...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
          props: {
              data: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              columns: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large']);
                  }
              },
3ef4dfb9   梁灏   update Table
88
89
90
              width: {
                  type: [Number, String]
              },
e7e8c8ff   梁灏   update Table
91
92
93
              height: {
                  type: [Number, String]
              },
2cb8a6d9   梁灏   commit Table comp...
94
95
96
97
98
99
100
101
              stripe: {
                  type: Boolean,
                  default: false
              },
              border: {
                  type: Boolean,
                  default: false
              },
2cb8a6d9   梁灏   commit Table comp...
102
103
104
105
              showHeader: {
                  type: Boolean,
                  default: true
              },
0d136465   梁灏   update Table
106
              highlightRow: {
2cb8a6d9   梁灏   commit Table comp...
107
108
                  type: Boolean,
                  default: false
e7e8c8ff   梁灏   update Table
109
110
111
112
113
114
              },
              rowClassName: {
                  type: Function,
                  default () {
                      return '';
                  }
2cb8a6d9   梁灏   commit Table comp...
115
116
117
118
              }
          },
          data () {
              return {
744eb0af   梁灏   update Table comp...
119
120
                  tableWidth: 0,
                  columnsWidth: [],
2cb8a6d9   梁灏   commit Table comp...
121
                  prefixCls: prefixCls,
0d136465   梁灏   update Table
122
                  compiledUids: [],
d3dfdb26   梁灏   update Table
123
                  objData: this.makeObjData(),
741b987a   梁灏   update Table
124
                  rebuildData: this.makeData(),    // for sort or filter
abdec99d   梁灏   update Table
125
                  cloneColumns: deepCopy(this.columns),
a3547c1b   梁灏   update Table
126
127
128
                  leftFixedColumns: [],
                  rightFixedColumns: [],
                  centerColumns: [],
e7e8c8ff   梁灏   update Table
129
130
131
                  showSlotHeader: true,
                  showSlotFooter: true,
                  bodyHeight: 0
2cb8a6d9   梁灏   commit Table comp...
132
133
134
135
136
137
138
              }
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
0d136465   梁灏   update Table
139
140
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-border`]: this.border,
e7e8c8ff   梁灏   update Table
141
142
143
144
                          [`${prefixCls}-stripe`]: this.stripe,
                          [`${prefixCls}-with-header`]: this.showSlotHeader,
                          [`${prefixCls}-with-footer`]: this.showSlotFooter,
                          [`${prefixCls}-with-fixed-top`]: !!this.height
2cb8a6d9   梁灏   commit Table comp...
145
146
                      }
                  ]
0d136465   梁灏   update Table
147
              },
e7e8c8ff   梁灏   update Table
148
149
150
              styles () {
                  let style = {};
                  if (!!this.height) style.height = `${this.height}px`;
3ef4dfb9   梁灏   update Table
151
                  if (!!this.width) style.width = `${this.width}px`;
e7e8c8ff   梁灏   update Table
152
153
                  return style;
              },
0d136465   梁灏   update Table
154
155
156
157
              tableStyle () {
                  let style = {};
                  if (this.tableWidth !== 0) style.width = `${this.tableWidth}px`;
                  return style;
e7e8c8ff   梁灏   update Table
158
              },
7f34c510   梁灏   update Table
159
160
161
162
163
164
165
166
167
168
              fixedTableStyle () {
                  let style = {};
                  if (this.leftFixedColumns.length) style.width = this.leftFixedColumns.reduce((a, b) => a + b);
                  return style;
              },
              fixedRightTableStyle () {
                  let style = {};
                  if (this.rightFixedColumns.length) style.width = this.rightFixedColumns.reduce((a, b) => a + b);
                  return style;
              },
e7e8c8ff   梁灏   update Table
169
170
171
172
              bodyStyle () {
                  let style = {};
                  if (this.bodyHeight !== 0) style.height = `${this.bodyHeight}px`;
                  return style;
b8a43000   梁灏   update Table
173
174
175
176
177
              },
              fixedBodyStyle () {
                  let style = {};
                  if (this.bodyHeight !== 0) style.height = `${this.bodyHeight - 1}px`;
                  return style;
2cb8a6d9   梁灏   commit Table comp...
178
179
180
              }
          },
          methods: {
e7e8c8ff   梁灏   update Table
181
182
183
              rowClsName (index) {
                  return this.rowClassName(this.data[index], index);
              },
a3547c1b   梁灏   update Table
184
              handleResize () {
2cb8a6d9   梁灏   commit Table comp...
185
                  this.$nextTick(() => {
a3547c1b   梁灏   update Table
186
187
188
189
190
                      const allWidth = !this.columns.some(cell => !cell.width);
                      if (allWidth) {
                          this.tableWidth = this.columns.map(cell => cell.width).reduce((a, b) => a + b);
                      } else {
                          this.tableWidth = parseInt(getStyle(this.$el, 'width')) - 1;
2cb8a6d9   梁灏   commit Table comp...
191
                      }
a3547c1b   梁灏   update Table
192
193
                      this.$nextTick(() => {
                          this.columnsWidth = [];
192e2cb8   梁灏   update Table
194
                          let autoWidthIndex = -1;
a3547c1b   梁灏   update Table
195
                          if (allWidth) autoWidthIndex = this.cloneColumns.findIndex(cell => !cell.width);
192e2cb8   梁灏   update Table
196
  
7f34c510   梁灏   update Table
197
                          const $td = this.$refs.tbody.$el.querySelectorAll('tbody tr')[0].querySelectorAll('td');
192e2cb8   梁灏   update Table
198
199
200
                          for (let i = 0; i < $td.length; i++) {    // can not use forEach in Firefox
                              if (i === autoWidthIndex) {
                                  this.columnsWidth.push(parseInt(getStyle($td[i], 'width')) - 1);
a3547c1b   梁灏   update Table
201
                              } else {
192e2cb8   梁灏   update Table
202
                                  this.columnsWidth.push(parseInt(getStyle($td[i], 'width')));
2cb8a6d9   梁灏   commit Table comp...
203
                              }
192e2cb8   梁灏   update Table
204
                          }
744eb0af   梁灏   update Table comp...
205
206
207
208
209
                      });
                  });
              },
              setCellWidth (column, index) {
                  return column.width ? column.width : this.columnsWidth[index];
0d136465   梁灏   update Table
210
              },
d3dfdb26   梁灏   update Table
211
212
213
              handleMouseIn (_index) {
                  if (this.objData[_index]._isHover) return;
                  this.objData[_index]._isHover = true;
abdec99d   梁灏   update Table
214
              },
d3dfdb26   梁灏   update Table
215
216
              handleMouseOut (_index) {
                  this.objData[_index]._isHover = false;
abdec99d   梁灏   update Table
217
              },
d3dfdb26   梁灏   update Table
218
219
              highlightCurrentRow (_index) {
                  if (!this.highlightRow || this.objData[_index]._isHighlight) return;
0d136465   梁灏   update Table
220
221
  
                  let oldIndex = -1;
d3dfdb26   梁灏   update Table
222
223
224
225
                  for (let i in this.objData) {
                      if (this.objData[i]._isHighlight) {
                          oldIndex = parseInt(i);
                          this.objData[i]._isHighlight = false;
0d136465   梁灏   update Table
226
                      }
d3dfdb26   梁灏   update Table
227
228
229
230
                  }
                  this.objData[_index]._isHighlight = true;
                  const oldData = oldIndex < 0 ? null : JSON.parse(JSON.stringify(this.data[oldIndex]));
                  this.$emit('on-current-change', JSON.parse(JSON.stringify(this.data[_index])), oldData);
0d136465   梁灏   update Table
231
232
233
              },
              getSelection () {
                  let selectionIndexes = [];
d3dfdb26   梁灏   update Table
234
235
236
                  for (let i in this.objData) {
                      if (this.objData[i]._isChecked) selectionIndexes.push(parseInt(i));
                  }
0d136465   梁灏   update Table
237
238
                  return JSON.parse(JSON.stringify(this.data.filter((data, index) => selectionIndexes.indexOf(index) > -1)));
              },
d3dfdb26   梁灏   update Table
239
              toggleSelect (_index) {
741b987a   梁灏   update Table
240
241
                  let data = {};
                  let index = -1;
d3dfdb26   梁灏   update Table
242
243
244
245
  
                  for (let i in this.objData) {
                      if (parseInt(i) === _index) {
                          data = this.objData[i];
741b987a   梁灏   update Table
246
                          index = i;
741b987a   梁灏   update Table
247
248
249
                      }
                  }
                  const status = !data._isChecked;
d3dfdb26   梁灏   update Table
250
251
  
                  this.objData[_index]._isChecked = status;
0d136465   梁灏   update Table
252
253
254
  
                  const selection = this.getSelection();
                  if (status) {
741b987a   梁灏   update Table
255
                      this.$emit('on-select', selection, JSON.parse(JSON.stringify(this.data[_index])));
0d136465   梁灏   update Table
256
257
258
                  }
                  this.$emit('on-selection-change', selection);
              },
3d9e4f20   梁灏   update Table
259
              selectAll (status) {
d3dfdb26   梁灏   update Table
260
261
262
                  for (let i in this.objData) {
                      this.objData[i]._isChecked = status;
                  }
3d9e4f20   梁灏   update Table
263
  
52874e27   梁灏   update Table
264
                  const selection = this.getSelection();
3d9e4f20   梁灏   update Table
265
                  if (status) {
52874e27   梁灏   update Table
266
                      this.$emit('on-select-all', selection);
3d9e4f20   梁灏   update Table
267
                  }
52874e27   梁灏   update Table
268
                  this.$emit('on-selection-change', selection);
e7e8c8ff   梁灏   update Table
269
270
271
272
273
274
275
276
277
278
              },
              fixedHeader () {
                  if (!!this.height) {
                      this.$nextTick(() => {
                          const titleHeight = parseInt(getStyle(this.$els.title, 'height')) || 0;
                          const headerHeight = parseInt(getStyle(this.$els.header, 'height')) || 0;
                          const footerHeight = parseInt(getStyle(this.$els.footer, 'height')) || 0;
                          this.bodyHeight = this.height - titleHeight - headerHeight - footerHeight;
                      })
                  }
abdec99d   梁灏   update Table
279
280
281
282
283
284
285
286
287
288
289
290
291
292
              },
              parseColumns () {
                  let left = [];
                  let right = [];
                  let center = [];
                  this.cloneColumns.forEach((col) => {
                      if (col.fixed && col.fixed === 'left') {
                          left.push(col);
                      } else if (col.fixed && col.fixed === 'right') {
                          right.push(col);
                      } else {
                          center.push(col);
                      }
                  });
a3547c1b   梁灏   update Table
293
294
295
                  this.leftFixedColumns = left;
                  this.rightFixedColumns = right;
                  this.centerColumns = center;
abdec99d   梁灏   update Table
296
                  this.cloneColumns = left.concat(center).concat(right);
192e2cb8   梁灏   update Table
297
298
              },
              handleBodyScroll (event) {
b8a43000   梁灏   update Table
299
300
301
                  if (this.showHeader) this.$els.header.scrollLeft = event.target.scrollLeft;
                  if (this.leftFixedColumns.length) this.$els.fixedBody.scrollTop = event.target.scrollTop;
                  if (this.rightFixedColumns.length) this.$els.fixedRightBody.scrollTop = event.target.scrollTop;
192e2cb8   梁灏   update Table
302
              },
3ef4dfb9   梁灏   update Table
303
304
305
306
307
308
309
310
311
              handleMouseWheel (event) {
                  const deltaX = event.deltaX;
                  const $body = this.$els.body;
  
                  if (deltaX > 0) {
                      $body.scrollLeft = $body.scrollLeft + 10;
                  } else {
                      $body.scrollLeft = $body.scrollLeft - 10;
                  }
52874e27   梁灏   update Table
312
313
314
              },
              handleSort (index, type) {
                  if (type === 'asc') {
741b987a   梁灏   update Table
315
316
317
                      this.rebuildData.sort((a, b) => {
                          return a.age > b.age;
                      })
52874e27   梁灏   update Table
318
319
                  } else if (type === 'desc') {
  
741b987a   梁灏   update Table
320
321
                  } else if (type === 'normal') {
                      this.rebuildData = this.makeData();
52874e27   梁灏   update Table
322
                  }
741b987a   梁灏   update Table
323
324
325
326
327
              },
              makeData () {
                  let data = deepCopy(this.data);
                  data.forEach((row, index) => row._index = index);
                  return data;
d3dfdb26   梁灏   update Table
328
329
330
331
332
333
334
335
336
337
338
              },
              makeObjData () {
                  let data = {};
                  this.data.forEach((row, index) => {
                      const newRow = deepCopy(row);// todo 直接替换
                      newRow._isHover = false;
                      newRow._isChecked = false;
                      newRow._isHighlight = false;
                      data[index] = newRow;
                  });
                  return data;
2cb8a6d9   梁灏   commit Table comp...
339
340
              }
          },
e7e8c8ff   梁灏   update Table
341
          compiled () {
abdec99d   梁灏   update Table
342
              this.parseColumns();
e7e8c8ff   梁灏   update Table
343
344
345
              this.showSlotHeader = this.$els.title.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== '';
              this.showSlotFooter = this.$els.footer.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== '';
          },
2cb8a6d9   梁灏   commit Table comp...
346
          ready () {
a3547c1b   梁灏   update Table
347
              this.handleResize();
e7e8c8ff   梁灏   update Table
348
              this.fixedHeader();
744eb0af   梁灏   update Table comp...
349
350
351
352
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
2cb8a6d9   梁灏   commit Table comp...
353
354
355
356
          },
          watch: {
              data: {
                  handler () {
d3dfdb26   梁灏   update Table
357
                      this.objData = this.makeObjData();
741b987a   梁灏   update Table
358
                      this.rebuildData = this.makeData();
a3547c1b   梁灏   update Table
359
                      this.handleResize();
2cb8a6d9   梁灏   commit Table comp...
360
361
362
363
364
                  },
                  deep: true
              },
              columns: {
                  handler () {
340681ab   梁灏   update Table
365
                      this.cloneColumns = deepCopy(this.columns);
abdec99d   梁灏   update Table
366
                      this.parseColumns();
a3547c1b   梁灏   update Table
367
                      this.handleResize();
2cb8a6d9   梁灏   commit Table comp...
368
369
                  },
                  deep: true
e7e8c8ff   梁灏   update Table
370
371
372
              },
              height () {
                  this.fixedHeader();
2cb8a6d9   梁灏   commit Table comp...
373
374
375
376
              }
          }
      }
  </script>