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