Blame view

src/components/table/table.vue 12.3 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)">
a3547c1b   梁灏   update Table
29
30
                              <div :class="[prefixCls + '-cell']" v-if="column.type === 'selection'">
                                  <Checkbox :checked="cloneData[index] && cloneData[index]._isChecked" @on-change="toggleSelect(index)"></Checkbox>
0d136465   梁灏   update Table
31
                              </div>
a3547c1b   梁灏   update Table
32
33
34
35
36
37
38
39
40
41
                              <Cell v-else :prefix-cls="prefixCls" :row="row" :column="column" :index="index"></Cell>
                              <!--<div :class="[prefixCls + '-cell']" v-else>-->
                                  <!--{{{ renderRow(row, column, index) }}}-->
                              <!--</div>-->
                              <!--<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>-->
0d136465   梁灏   update Table
42
                          </td>
2cb8a6d9   梁灏   commit Table comp...
43
44
45
46
                      </tr>
                  </tbody>
              </table>
          </div>
abdec99d   梁灏   update Table
47
48
49
50
51
52
          <div :class="[prefixCls + '-fixed']">
  
          </div>
          <div :class="[prefixCls + '-fixed-right']">
  
          </div>
e7e8c8ff   梁灏   update Table
53
          <div :class="[prefixCls + '-footer']" v-if="showSlotFooter" v-el:footer><slot name="footer"></slot></div>
2cb8a6d9   梁灏   commit Table comp...
54
55
56
57
      </div>
  </template>
  <script>
      import TableHead from './table-head.vue';
0d136465   梁灏   update Table
58
      import Checkbox from '../checkbox/checkbox.vue';
a3547c1b   梁灏   update Table
59
      import Cell from './cell.vue';
0d136465   梁灏   update Table
60
61
      import Mixin from './mixin';
      import { oneOf, getStyle, deepCopy } from '../../utils/assist';
2cb8a6d9   梁灏   commit Table comp...
62
63
64
      const prefixCls = 'ivu-table';
  
      export default {
0d136465   梁灏   update Table
65
          mixins: [ Mixin ],
a3547c1b   梁灏   update Table
66
          components: { TableHead, Checkbox, Cell },
2cb8a6d9   梁灏   commit Table comp...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
          props: {
              data: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              columns: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large']);
                  }
              },
e7e8c8ff   梁灏   update Table
85
86
87
              height: {
                  type: [Number, String]
              },
2cb8a6d9   梁灏   commit Table comp...
88
89
90
91
92
93
94
95
              stripe: {
                  type: Boolean,
                  default: false
              },
              border: {
                  type: Boolean,
                  default: false
              },
2cb8a6d9   梁灏   commit Table comp...
96
97
98
99
              showHeader: {
                  type: Boolean,
                  default: true
              },
0d136465   梁灏   update Table
100
              highlightRow: {
2cb8a6d9   梁灏   commit Table comp...
101
102
                  type: Boolean,
                  default: false
e7e8c8ff   梁灏   update Table
103
104
105
106
107
108
              },
              rowClassName: {
                  type: Function,
                  default () {
                      return '';
                  }
2cb8a6d9   梁灏   commit Table comp...
109
110
111
112
              }
          },
          data () {
              return {
744eb0af   梁灏   update Table comp...
113
114
                  tableWidth: 0,
                  columnsWidth: [],
2cb8a6d9   梁灏   commit Table comp...
115
                  prefixCls: prefixCls,
0d136465   梁灏   update Table
116
                  compiledUids: [],
e7e8c8ff   梁灏   update Table
117
                  cloneData: deepCopy(this.data),
abdec99d   梁灏   update Table
118
                  cloneColumns: deepCopy(this.columns),
a3547c1b   梁灏   update Table
119
120
121
                  leftFixedColumns: [],
                  rightFixedColumns: [],
                  centerColumns: [],
e7e8c8ff   梁灏   update Table
122
123
124
                  showSlotHeader: true,
                  showSlotFooter: true,
                  bodyHeight: 0
2cb8a6d9   梁灏   commit Table comp...
125
126
127
128
129
130
131
              }
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
0d136465   梁灏   update Table
132
133
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-border`]: this.border,
e7e8c8ff   梁灏   update Table
134
135
136
137
                          [`${prefixCls}-stripe`]: this.stripe,
                          [`${prefixCls}-with-header`]: this.showSlotHeader,
                          [`${prefixCls}-with-footer`]: this.showSlotFooter,
                          [`${prefixCls}-with-fixed-top`]: !!this.height
2cb8a6d9   梁灏   commit Table comp...
138
139
                      }
                  ]
0d136465   梁灏   update Table
140
              },
e7e8c8ff   梁灏   update Table
141
142
143
144
145
              styles () {
                  let style = {};
                  if (!!this.height) style.height = `${this.height}px`;
                  return style;
              },
0d136465   梁灏   update Table
146
147
148
149
              tableStyle () {
                  let style = {};
                  if (this.tableWidth !== 0) style.width = `${this.tableWidth}px`;
                  return style;
e7e8c8ff   梁灏   update Table
150
151
152
153
154
              },
              bodyStyle () {
                  let style = {};
                  if (this.bodyHeight !== 0) style.height = `${this.bodyHeight}px`;
                  return style;
2cb8a6d9   梁灏   commit Table comp...
155
156
157
              }
          },
          methods: {
e7e8c8ff   梁灏   update Table
158
159
160
              rowClsName (index) {
                  return this.rowClassName(this.data[index], index);
              },
a3547c1b   梁灏   update Table
161
              handleResize () {
2cb8a6d9   梁灏   commit Table comp...
162
                  this.$nextTick(() => {
a3547c1b   梁灏   update Table
163
164
165
166
167
                      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...
168
                      }
a3547c1b   梁灏   update Table
169
170
171
172
173
174
175
176
177
                      this.$nextTick(() => {
                          this.columnsWidth = [];
                          let autoWidthIndex = -1
                          if (allWidth) autoWidthIndex = this.cloneColumns.findIndex(cell => !cell.width);
                          this.$els.tbody.querySelectorAll('tbody tr')[0].querySelectorAll('td').forEach((cell, index) => {
                              if (index === autoWidthIndex) {
                                  this.columnsWidth.push(parseInt(getStyle(cell, 'width')) - 1);
                              } else {
                                  this.columnsWidth.push(parseInt(getStyle(cell, 'width')));
2cb8a6d9   梁灏   commit Table comp...
178
                              }
a3547c1b   梁灏   update Table
179
                          });
744eb0af   梁灏   update Table comp...
180
181
182
183
184
                      });
                  });
              },
              setCellWidth (column, index) {
                  return column.width ? column.width : this.columnsWidth[index];
0d136465   梁灏   update Table
185
              },
abdec99d   梁灏   update Table
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
              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
202
203
204
205
206
207
208
209
210
211
212
              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
213
                  const row = this.assignRow(index, {
0d136465   梁灏   update Table
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
                      _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
231
                  const row = this.assignRow(index, {
0d136465   梁灏   update Table
232
233
234
235
236
237
238
239
240
241
242
243
                      _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
244
245
246
247
248
249
250
251
252
253
              },
              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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
              },
              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
268
269
270
                  this.leftFixedColumns = left;
                  this.rightFixedColumns = right;
                  this.centerColumns = center;
abdec99d   梁灏   update Table
271
                  this.cloneColumns = left.concat(center).concat(right);
2cb8a6d9   梁灏   commit Table comp...
272
273
              }
          },
e7e8c8ff   梁灏   update Table
274
          compiled () {
abdec99d   梁灏   update Table
275
              this.parseColumns();
e7e8c8ff   梁灏   update Table
276
277
278
              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...
279
          ready () {
a3547c1b   梁灏   update Table
280
              this.handleResize();
e7e8c8ff   梁灏   update Table
281
              this.fixedHeader();
744eb0af   梁灏   update Table comp...
282
283
284
285
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
2cb8a6d9   梁灏   commit Table comp...
286
287
288
289
          },
          watch: {
              data: {
                  handler () {
0d136465   梁灏   update Table
290
                      this.cloneData = deepCopy(this.data);
a3547c1b   梁灏   update Table
291
                      this.handleResize();
2cb8a6d9   梁灏   commit Table comp...
292
293
294
295
296
                  },
                  deep: true
              },
              columns: {
                  handler () {
abdec99d   梁灏   update Table
297
                      this.parseColumns();
a3547c1b   梁灏   update Table
298
                      this.handleResize();
2cb8a6d9   梁灏   commit Table comp...
299
300
                  },
                  deep: true
e7e8c8ff   梁灏   update Table
301
302
303
              },
              height () {
                  this.fixedHeader();
2cb8a6d9   梁灏   commit Table comp...
304
305
306
307
              }
          }
      }
  </script>