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