Blame view

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