Blame view

src/components/table/table.vue 13 KB
2cb8a6d9   梁灏   commit Table comp...
1
  <template>
e7e8c8ff   梁灏   update Table
2
3
4
      <div :class="classes" :style="styles">
          <div :class="[prefixCls + '-title']" v-if="showSlotHeader" v-el:title><slot name="header"></slot></div>
          <div :class="[prefixCls + '-header']" v-if="showHeader" v-el:header>
0d136465   梁灏   update Table
5
              <table cellspacing="0" cellpadding="0" border="0" :style="tableStyle">
2cb8a6d9   梁灏   commit Table comp...
6
                  <colgroup>
abdec99d   梁灏   update Table
7
                      <col v-for="column in cloneColumns" :width="setCellWidth(column, $index)">
2cb8a6d9   梁灏   commit Table comp...
8
9
10
                  </colgroup>
                  <thead
                      is="table-head"
0d136465   梁灏   update Table
11
12
                      :prefix-cls="prefixCls"
                      :clone-data.sync="cloneData"
abdec99d   梁灏   update Table
13
                      :columns="cloneColumns"></thead>
744eb0af   梁灏   update Table comp...
14
15
              </table>
          </div>
e7e8c8ff   梁灏   update Table
16
          <div :class="[prefixCls + '-body']" :style="bodyStyle">
0d136465   梁灏   update Table
17
              <table cellspacing="0" cellpadding="0" border="0" :style="tableStyle" v-el:tbody>
744eb0af   梁灏   update Table comp...
18
                  <colgroup>
abdec99d   梁灏   update Table
19
                      <col v-for="column in cloneColumns" :width="setCellWidth(column, $index)">
744eb0af   梁灏   update Table comp...
20
                  </colgroup>
2cb8a6d9   梁灏   commit Table comp...
21
                  <tbody :class="[prefixCls + '-tbody']" v-el:render>
0d136465   梁灏   update Table
22
23
                      <tr
                          v-for="(index, row) in data"
abdec99d   梁灏   update Table
24
25
26
                          :class="[prefixCls + '-row', rowClsName(index), {[prefixCls + '-row-highlight']: cloneData[index] && cloneData[index]._isHighlight, [prefixCls + '-row-hover']: cloneData[index] && cloneData[index]._isHover}]"
                          @mouseenter.stop="handleMouseIn(index)"
                          @mouseleave.stop="handleMouseOut(index)"
0d136465   梁灏   update Table
27
                          @click.stop="highlightCurrentRow(index)">
abdec99d   梁灏   update Table
28
                          <td v-for="column in cloneColumns" :class="alignCls(column)">
0d136465   梁灏   update Table
29
30
31
32
33
34
35
                              <div :class="[prefixCls + '-cell']">
                                  <template v-if="column.type === 'selection'">
                                      <Checkbox :checked="cloneData[index] && cloneData[index]._isChecked" @on-change="toggleSelect(index)"></Checkbox>
                                  </template>
                                  <template v-else>{{{ renderRow(row, column, index) }}}</template>
                              </div>
                          </td>
2cb8a6d9   梁灏   commit Table comp...
36
37
38
39
                      </tr>
                  </tbody>
              </table>
          </div>
abdec99d   梁灏   update Table
40
41
42
43
44
45
          <div :class="[prefixCls + '-fixed']">
  
          </div>
          <div :class="[prefixCls + '-fixed-right']">
  
          </div>
e7e8c8ff   梁灏   update Table
46
          <div :class="[prefixCls + '-footer']" v-if="showSlotFooter" v-el:footer><slot name="footer"></slot></div>
2cb8a6d9   梁灏   commit Table comp...
47
48
49
50
      </div>
  </template>
  <script>
      import TableHead from './table-head.vue';
0d136465   梁灏   update Table
51
52
53
      import Checkbox from '../checkbox/checkbox.vue';
      import Mixin from './mixin';
      import { oneOf, getStyle, deepCopy } from '../../utils/assist';
2cb8a6d9   梁灏   commit Table comp...
54
55
56
      const prefixCls = 'ivu-table';
  
      export default {
0d136465   梁灏   update Table
57
58
          mixins: [ Mixin ],
          components: { TableHead, Checkbox },
2cb8a6d9   梁灏   commit Table comp...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
          props: {
              data: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              columns: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large']);
                  }
              },
e7e8c8ff   梁灏   update Table
77
78
79
              height: {
                  type: [Number, String]
              },
2cb8a6d9   梁灏   commit Table comp...
80
81
82
83
84
85
86
87
              stripe: {
                  type: Boolean,
                  default: false
              },
              border: {
                  type: Boolean,
                  default: false
              },
2cb8a6d9   梁灏   commit Table comp...
88
89
90
91
              showHeader: {
                  type: Boolean,
                  default: true
              },
0d136465   梁灏   update Table
92
              highlightRow: {
2cb8a6d9   梁灏   commit Table comp...
93
94
                  type: Boolean,
                  default: false
e7e8c8ff   梁灏   update Table
95
96
97
98
99
100
              },
              rowClassName: {
                  type: Function,
                  default () {
                      return '';
                  }
2cb8a6d9   梁灏   commit Table comp...
101
102
103
104
              }
          },
          data () {
              return {
744eb0af   梁灏   update Table comp...
105
106
                  tableWidth: 0,
                  columnsWidth: [],
2cb8a6d9   梁灏   commit Table comp...
107
                  prefixCls: prefixCls,
0d136465   梁灏   update Table
108
                  compiledUids: [],
e7e8c8ff   梁灏   update Table
109
                  cloneData: deepCopy(this.data),
abdec99d   梁灏   update Table
110
                  cloneColumns: deepCopy(this.columns),
e7e8c8ff   梁灏   update Table
111
112
113
                  showSlotHeader: true,
                  showSlotFooter: true,
                  bodyHeight: 0
2cb8a6d9   梁灏   commit Table comp...
114
115
116
117
118
119
120
              }
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
0d136465   梁灏   update Table
121
122
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-border`]: this.border,
e7e8c8ff   梁灏   update Table
123
124
125
126
                          [`${prefixCls}-stripe`]: this.stripe,
                          [`${prefixCls}-with-header`]: this.showSlotHeader,
                          [`${prefixCls}-with-footer`]: this.showSlotFooter,
                          [`${prefixCls}-with-fixed-top`]: !!this.height
2cb8a6d9   梁灏   commit Table comp...
127
128
                      }
                  ]
0d136465   梁灏   update Table
129
              },
e7e8c8ff   梁灏   update Table
130
131
132
133
134
              styles () {
                  let style = {};
                  if (!!this.height) style.height = `${this.height}px`;
                  return style;
              },
0d136465   梁灏   update Table
135
136
137
138
              tableStyle () {
                  let style = {};
                  if (this.tableWidth !== 0) style.width = `${this.tableWidth}px`;
                  return style;
e7e8c8ff   梁灏   update Table
139
140
141
142
143
              },
              bodyStyle () {
                  let style = {};
                  if (this.bodyHeight !== 0) style.height = `${this.bodyHeight}px`;
                  return style;
2cb8a6d9   梁灏   commit Table comp...
144
145
146
              }
          },
          methods: {
e7e8c8ff   梁灏   update Table
147
148
149
              rowClsName (index) {
                  return this.rowClassName(this.data[index], index);
              },
0d136465   梁灏   update Table
150
151
              renderRow (row, column, index) {
                  return column.type === 'index' ? index + 1 : column.render ? '' : row[column.key];
2cb8a6d9   梁灏   commit Table comp...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
              },
              compileRender (update = false) {
                  this.$nextTick(() => {
                      if (update) {
                          for (let i = 0; i < this.$parent.$children.length; i++) {
                              const index = this.compiledUids.indexOf(this.$parent.$children[i]._uid);
                              if (index > -1) {
                                  this.$parent.$children[i].$destroy();
                                  this.compiledUids.splice(index, 1);
                                  i--;
                              }
                          }
                      }
  
                      const $el = this.$els.render;
abdec99d   梁灏   update Table
167
168
                      for (let i = 0; i < this.cloneColumns.length; i++) {
                          const column = this.cloneColumns[i];
2cb8a6d9   梁灏   commit Table comp...
169
170
                          if (column.render) {
                              for (let j = 0; j < this.data.length; j++) {
0d136465   梁灏   update Table
171
                                  // todo 做一个缓存,只在需要改render时再重新编译,data改变时不用再编译
2cb8a6d9   梁灏   commit Table comp...
172
173
174
175
176
177
178
179
180
181
182
                                  const row = this.data[j];
                                  const template = column.render(row, column, j);
                                  const cell = document.createElement('div');
                                  cell.innerHTML = template;
                                  const _oldParentChildLen = this.$parent.$children.length;
                                  this.$parent.$compile(cell);
                                  const _newParentChildLen = this.$parent.$children.length;
  
                                  if (_oldParentChildLen !== _newParentChildLen) {    // if render normal html node, do not tag
                                      this.compiledUids.push(this.$parent.$children[this.$parent.$children.length - 1]._uid);    // tag it, and delete when data or columns update
                                  }
0d136465   梁灏   update Table
183
184
                                  $el.children[j].children[i].children[0].innerHTML = '';
                                  $el.children[j].children[i].children[0].appendChild(cell);
2cb8a6d9   梁灏   commit Table comp...
185
186
187
                              }
                          }
                      }
744eb0af   梁灏   update Table comp...
188
                      this.handleResize();
2cb8a6d9   梁灏   commit Table comp...
189
                  });
744eb0af   梁灏   update Table comp...
190
191
192
193
194
195
196
197
198
199
200
201
              },
              handleResize () {
                  this.tableWidth = parseInt(getStyle(this.$el, 'width'));
                  this.$nextTick(() => {
                      this.columnsWidth = [];
                      this.$els.tbody.querySelectorAll('tbody tr')[0].querySelectorAll('td').forEach((cell) => {
                          this.columnsWidth.push(parseInt(getStyle(cell, 'width')));
                      });
                  });
              },
              setCellWidth (column, index) {
                  return column.width ? column.width : this.columnsWidth[index];
0d136465   梁灏   update Table
202
              },
abdec99d   梁灏   update Table
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
              assignRow (index, obj) {
                  return Object.assign({}, this.cloneData[index], obj);
              },
              handleMouseIn (index) {
                  if (this.cloneData[index]._isHover) return;
                  const row = this.assignRow(index, {
                      _isHover: true
                  });
                  this.cloneData.$set(index, row);
              },
              handleMouseOut (index) {
                  const row = this.assignRow(index, {
                      _isHover: false
                  });
                  this.cloneData.$set(index, row);
              },
0d136465   梁灏   update Table
219
220
221
222
223
224
225
226
227
228
229
              highlightCurrentRow (index) {
                  if (!this.highlightRow || this.cloneData[index]._isHighlight) return;
  
                  let oldIndex = -1;
                  this.cloneData.forEach((item, index) => {
                      if (item._isHighlight) {
                          oldIndex = index;
                          item._isHighlight = false;
                          return true;
                      }
                  });
abdec99d   梁灏   update Table
230
                  const row = this.assignRow(index, {
0d136465   梁灏   update Table
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
                      _isHighlight: true
                  });
                  this.cloneData.$set(index, row);
  
                  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);
              },
              getSelection () {
                  let selectionIndexes = [];
                  this.cloneData.forEach((data, index) => {
                      if (data._isChecked) selectionIndexes.push(index);
                  });
  
                  return JSON.parse(JSON.stringify(this.data.filter((data, index) => selectionIndexes.indexOf(index) > -1)));
              },
              toggleSelect (index) {
                  const status = !this.cloneData[index]._isChecked;
abdec99d   梁灏   update Table
248
                  const row = this.assignRow(index, {
0d136465   梁灏   update Table
249
250
251
252
253
254
255
256
257
258
259
260
                      _isChecked: status
                  });
                  this.cloneData.$set(index, row);
  
                  const selection = this.getSelection();
                  if (status) {
                      this.$emit('on-select', selection, JSON.parse(JSON.stringify(this.data[index])));
                  }
                  this.$emit('on-selection-change', selection);
              },
              selectAll () {
                  this.$emit('on-select-all', this.getSelection());
e7e8c8ff   梁灏   update Table
261
262
263
264
265
266
267
268
269
270
              },
              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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
              },
              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);
                      }
                  });
                  this.cloneColumns = left.concat(center).concat(right);
2cb8a6d9   梁灏   commit Table comp...
286
287
              }
          },
e7e8c8ff   梁灏   update Table
288
          compiled () {
abdec99d   梁灏   update Table
289
              this.parseColumns();
e7e8c8ff   梁灏   update Table
290
291
292
              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...
293
294
          ready () {
              this.compileRender();
e7e8c8ff   梁灏   update Table
295
              this.fixedHeader();
744eb0af   梁灏   update Table comp...
296
297
298
299
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
2cb8a6d9   梁灏   commit Table comp...
300
301
302
303
          },
          watch: {
              data: {
                  handler () {
0d136465   梁灏   update Table
304
                      this.cloneData = deepCopy(this.data);
2cb8a6d9   梁灏   commit Table comp...
305
306
307
308
309
310
                      this.compileRender(true);
                  },
                  deep: true
              },
              columns: {
                  handler () {
abdec99d   梁灏   update Table
311
                      this.parseColumns();
2cb8a6d9   梁灏   commit Table comp...
312
313
314
                      this.compileRender(true);
                  },
                  deep: true
e7e8c8ff   梁灏   update Table
315
316
317
              },
              height () {
                  this.fixedHeader();
2cb8a6d9   梁灏   commit Table comp...
318
319
320
321
              }
          }
      }
  </script>