Blame view

src/components/table/table.vue 9.2 KB
2cb8a6d9   梁灏   commit Table comp...
1
2
  <template>
      <div :class="classes">
0d136465   梁灏   update Table
3
4
          <div :class="[prefixCls + '-header']" v-if="showHeader">
              <table cellspacing="0" cellpadding="0" border="0" :style="tableStyle">
2cb8a6d9   梁灏   commit Table comp...
5
                  <colgroup>
744eb0af   梁灏   update Table comp...
6
                      <col v-for="column in columns" :width="setCellWidth(column, $index)">
2cb8a6d9   梁灏   commit Table comp...
7
8
9
                  </colgroup>
                  <thead
                      is="table-head"
0d136465   梁灏   update Table
10
11
                      :prefix-cls="prefixCls"
                      :clone-data.sync="cloneData"
2cb8a6d9   梁灏   commit Table comp...
12
                      :columns="columns"></thead>
744eb0af   梁灏   update Table comp...
13
14
15
              </table>
          </div>
          <div :class="[prefixCls + '-body']">
0d136465   梁灏   update Table
16
              <table cellspacing="0" cellpadding="0" border="0" :style="tableStyle" v-el:tbody>
744eb0af   梁灏   update Table comp...
17
18
19
                  <colgroup>
                      <col v-for="column in columns" :width="setCellWidth(column, $index)">
                  </colgroup>
2cb8a6d9   梁灏   commit Table comp...
20
                  <tbody :class="[prefixCls + '-tbody']" v-el:render>
0d136465   梁灏   update Table
21
22
23
24
25
26
27
28
29
30
31
32
                      <tr
                          v-for="(index, row) in data"
                          :class="[prefixCls + '-row', {[prefixCls + '-row-highlight']: cloneData[index] && cloneData[index]._isHighlight}]"
                          @click.stop="highlightCurrentRow(index)">
                          <td v-for="column in columns" :class="alignCls(column)">
                              <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...
33
34
35
36
37
38
39
40
                      </tr>
                  </tbody>
              </table>
          </div>
      </div>
  </template>
  <script>
      import TableHead from './table-head.vue';
0d136465   梁灏   update Table
41
42
43
      import Checkbox from '../checkbox/checkbox.vue';
      import Mixin from './mixin';
      import { oneOf, getStyle, deepCopy } from '../../utils/assist';
2cb8a6d9   梁灏   commit Table comp...
44
45
46
      const prefixCls = 'ivu-table';
  
      export default {
0d136465   梁灏   update Table
47
48
          mixins: [ Mixin ],
          components: { TableHead, Checkbox },
2cb8a6d9   梁灏   commit Table comp...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
          props: {
              data: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              columns: {
                  type: Array,
                  default () {
                      return []
                  }
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large']);
                  }
              },
              stripe: {
                  type: Boolean,
                  default: false
              },
              border: {
                  type: Boolean,
                  default: false
              },
2cb8a6d9   梁灏   commit Table comp...
75
76
77
78
              showHeader: {
                  type: Boolean,
                  default: true
              },
0d136465   梁灏   update Table
79
              highlightRow: {
2cb8a6d9   梁灏   commit Table comp...
80
81
82
83
84
85
                  type: Boolean,
                  default: false
              }
          },
          data () {
              return {
744eb0af   梁灏   update Table comp...
86
87
                  tableWidth: 0,
                  columnsWidth: [],
2cb8a6d9   梁灏   commit Table comp...
88
                  prefixCls: prefixCls,
0d136465   梁灏   update Table
89
90
                  compiledUids: [],
                  cloneData: deepCopy(this.data)
2cb8a6d9   梁灏   commit Table comp...
91
92
93
94
95
96
97
              }
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
0d136465   梁灏   update Table
98
99
100
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-border`]: this.border,
                          [`${prefixCls}-stripe`]: this.stripe
2cb8a6d9   梁灏   commit Table comp...
101
102
                      }
                  ]
0d136465   梁灏   update Table
103
104
105
106
107
              },
              tableStyle () {
                  let style = {};
                  if (this.tableWidth !== 0) style.width = `${this.tableWidth}px`;
                  return style;
2cb8a6d9   梁灏   commit Table comp...
108
109
110
              }
          },
          methods: {
0d136465   梁灏   update Table
111
112
              renderRow (row, column, index) {
                  return column.type === 'index' ? index + 1 : column.render ? '' : row[column.key];
2cb8a6d9   梁灏   commit Table comp...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
              },
              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;
                      for (let i = 0; i < this.columns.length; i++) {
                          const column = this.columns[i];
                          if (column.render) {
                              for (let j = 0; j < this.data.length; j++) {
0d136465   梁灏   update Table
132
                                  // todo 做一个缓存,只在需要改render时再重新编译,data改变时不用再编译
2cb8a6d9   梁灏   commit Table comp...
133
134
135
136
137
138
139
140
141
142
143
                                  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
144
145
                                  $el.children[j].children[i].children[0].innerHTML = '';
                                  $el.children[j].children[i].children[0].appendChild(cell);
2cb8a6d9   梁灏   commit Table comp...
146
147
148
                              }
                          }
                      }
744eb0af   梁灏   update Table comp...
149
                      this.handleResize();
2cb8a6d9   梁灏   commit Table comp...
150
                  });
744eb0af   梁灏   update Table comp...
151
152
153
154
155
156
157
158
159
160
161
162
              },
              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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
              },
              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;
                      }
                  });
                  const row = Object.assign({}, this.cloneData[index], {
                      _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;
                  const row = Object.assign({}, this.cloneData[index], {
                      _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());
2cb8a6d9   梁灏   commit Table comp...
206
207
208
209
              }
          },
          ready () {
              this.compileRender();
744eb0af   梁灏   update Table comp...
210
211
212
213
              window.addEventListener('resize', this.handleResize, false);
          },
          beforeDestroy () {
              window.removeEventListener('resize', this.handleResize, false);
2cb8a6d9   梁灏   commit Table comp...
214
215
216
217
          },
          watch: {
              data: {
                  handler () {
0d136465   梁灏   update Table
218
                      this.cloneData = deepCopy(this.data);
2cb8a6d9   梁灏   commit Table comp...
219
220
221
222
223
224
225
226
227
228
229
230
231
                      this.compileRender(true);
                  },
                  deep: true
              },
              columns: {
                  handler () {
                      this.compileRender(true);
                  },
                  deep: true
              }
          }
      }
  </script>