-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
489 lines (459 loc) · 11.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/init.css">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
html,
body {
height: auto
}
* {
margin: 0;
padding: 0;
border: 0;
box-sizing: border-box;
}
.all-table-wrap {
width: 100%;
position: relative;
cursor: default;
}
.all-table-wrap thead th {
background-color: #f5f7f9;
height: 40px;
}
.table-style th {
color: #657180;
font-size: 13px;
border: 1px solid #e3e8ee;
}
.tr-hover {
background-color: #ebf7ff !important;
}
.table-style tbody tr:nth-child(even) {
background: #f5f7f9;
}
.table-style td {
padding: 8px 8px;
color: #657180;
font-size: 12px;
text-align: center;
box-sizing: border-box;
border: 1px solid #e3e8ee;
}
.table-wrap {
width: 100%;
word-break: keep-all;
white-space: nowrap;
overflow: auto;
position: relative;
}
/* 表头固定 */
.fixRow-table {
background-color: #fff;
position: relative;
overflow: hidden;
margin-right: 10px;
z-index: 22;
}
/*真实表格*/
.table-t {
width: 100%;
}
.table-t thead th {
padding: 0 8px;
border-bottom: none;
}
/*滚动条的设置*/
.table-t tbody tr:first-child>td {
border-top: none;
}
.table-wrap::-webkit-scrollbar {
width: 10px;
height: 10px;
background-color: #f5f5f5;
}
/*.table-wrap:hover::-webkit-scrollbar {
width: 10px;
height: 10px;
background-color: #f5f5f5;
}*/
.table-wrap::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 1 6px rgba(0, 0, 0, .3);
border-radius: 5px;
background-color: #f5f5f5;
}
.table-wrap::-webkit-scrollbar-thumb {
/* 滚动条中间滚动的颜色 */
border-radius: 5px;
-webkit-box-shadow: inset 0 1 6px rgba(0, 0, 0, .3);
background-color: #d2d2d2;
}
.table-wrap::-webkit-scrollbar-corner {
/* 滚动条中间滚动的颜色 */
background-color: #f5f5f5;
}
.table-fix-tbody-wrap {
top: -40px;
position: relative;
}
.fixLeftCol-table-wrap-title {
position: absolute;
top: 0;
background-color: #fff;
z-index: 99;
}
/*表体左侧*/
.fixLeftCol-table-wrap {
overflow: hidden;
position: absolute;
background-color: #fff;
z-index: 33;
top: 0;
box-shadow: 2px 0 6px -2px rgba(0, 0, 0, .2);
}
/*
.fixLeftCol-table-wrap tr {
box-shadow: 2px 0 6px -2px rgba(0, 0, 0, .2);
}*/
.fixRightCol-table-wrap-title {
position: absolute;
top: 0;
right: 10px;
background-color: #fff;
z-index: 99;
}
.fixRightCol-table-wrap {
overflow: hidden;
position: absolute;
background-color: #fff;
z-index: 33;
top: 0;
right: 10px;
box-shadow: -2px 0 6px -2px rgba(0, 0, 0, .2);
}
/*当数据不是特别多,右边没有滚动条时*/
.no-left-scroll {
right: 0;
}
</style>
</head>
<body>
<div id="app">
<div style="width:1300px;margin:16px auto">
<vue-fix-table :table-Height="tableHeight" :table-Data="tableData" :table-Columns="tableColumns" @t-dblclick="tableClick">
</vue-fix-table>
</div>
</div>
</body>
<template id="vue-fix-table">
<!--表格-->
<div class="all-table-wrap" :style="{height:tableHeight+'px'}">
<!--固定顶部表头-->
<div class="fixRow-table" ref="tableTop">
<table class="table-style">
<thead>
<tr>
<th v-for="col in tableColumnsFix.allCol">
<div :style="{width:col.width+'px'}">
{{col.title}}
</div>
</th>
</tr>
</thead>
</table>
</div>
<!--固定左侧表头-->
<template v-if="(tableData.length>0&&tableColumnsFix.fixLeftCol.length>0)">
<div class="fixLeftCol-table-wrap-title">
<table class="table-style">
<thead>
<tr>
<th v-for="th in tableColumnsFix.fixLeftCol">
<div :style="{width:th.width+'px'}">
{{th.title}}
</div>
</th>
</tr>
</thead>
</table>
</div>
<!--固定左侧的数据-->
<div class="fixLeftCol-table-wrap" ref="tableLeft" :style="{height:tableFixHeight}">
<table class="table-style">
<thead>
<tr>
<th v-for="th in tableColumnsFix.fixLeftCol">
<div :style="{width:th.width+'px'}">
{{th.title}}
</div>
</th>
</tr>
</thead>
<tbody>
<tr v-for="(rowData,index) in tableData" :class="{'tr-hover':nowHover==index}" @mouseover="trHover(index)" @mouseout="trOut">
<td v-for="th in tableColumnsFix.fixLeftCol">{{rowData[th.key]}}</td>
</tr>
</tbody>
</table>
</div>
</template>
<!--真实表格-->
<div class="table-fix-tbody-wrap">
<div class="table-wrap" @scroll="tableScroll" ref="table" :style="{height:tableHeight+'px'}">
<table class="table-style table-t" ref="tableTrue">
<thead>
<tr>
<th v-for="col in tableColumnsFix.allCol" style="min-width:120px">
{{col.title}}
</th>
</tr>
</thead>
<tbody>
<!--如果没有数据-->
<template v-if="tableData.length==0">
<tr>
<td :colspan="tableColumnsFix.allCol.length" style="text-align:center">暂无数据</td>
</tr>
</template>
<tr v-for="(rowData,index) in tableData" @dblclick="dblclick(rowData,index)" :class="{'tr-hover':nowHover==index}" @mouseover="trHover(index)"
@mouseout="trOut">
<td v-for="col in tableColumnsFix.allCol">{{rowData[col.key]}}</td>
</tr>
</tbody>
</table>
</div>
</div>
<!--固定右侧表头-->
<template v-if="(tableData.length>0&&tableColumnsFix.fixRightCol.length>0)">
<div class="fixRightCol-table-wrap-title" :class="{'no-left-scroll':noLeftScroll}">
<table class="table-style">
<thead>
<tr>
<th v-for="th in tableColumnsFix.fixRightCol">
<div :style="{width:th.width+'px'}">
{{th.title}}
</div>
</th>
</tr>
</thead>
</table>
</div>
<!--固定右侧-->
<div class="fixRightCol-table-wrap" ref="tableRight" :style="{height:tableFixHeight}" :class="{'no-left-scroll':noLeftScroll}">
<table class="table-style">
<thead>
<tr>
<th v-for="th in tableColumnsFix.fixRightCol">
<div :style="{width:th.width+'px'}">
{{th.title}}
</div>
</th>
</tr>
</thead>
<tbody>
<tr v-for="(rowData,index) in tableData" :class="{'tr-hover':nowHover==index}" @mouseover="trHover(index)" @mouseout="trOut">
<td v-for="th in tableColumnsFix.fixRightCol">{{rowData[th.key]}}</td>
</tr>
</tbody>
</table>
</div>
</template>
<!--end -->
</div>
</template>
<script src="http://cdn.bootcss.com/vue/2.1.10/vue.js"></script>
<script>
"use strict"
Vue.component('vue-fix-table', {
template: '#vue-fix-table',
props: {
tableHeight: Number, //表格高度
tableData: Array, //表格数据
tableColumns: Array //表格表头
},
data: function () {
return {
noLeftScroll: '',
tableFixHeight: '0px',
nowHover: -1 //当前经过的行
}
},
watch: {
//表格数据更新完毕,固定表格
tableData: function (val, oldVal) {
this.$nextTick(function () {
this.checkFix();
this.CalcTableFixHeight();
})
},
tableColumns: function (val, oldVal) {
this.$nextTick(function () {
this.checkFix();
this.CalcTableFixHeight();
})
}
},
mounted: function () {
this.checkFix();
this.CalcTableFixHeight();
//监听窗口改变事件
var self = this;
window.addEventListener('resize', function () {
self.checkFix();
self.CalcTableFixHeight();
});
},
computed: {
// 固定列的数据
tableColumnsFix: function () {
var fixLeftCol = [];
var fixRightCol = [];
var unFixCol = [];
for (var i = 0; i < this.tableColumns.length; i++) {
var thisData = this.tableColumns[i];
if (thisData.fixed && thisData.fixed == 'left') {
fixLeftCol.push(thisData);
} else if (thisData.fixed && thisData.fixed == 'right') {
fixRightCol.push(thisData);
} else {
unFixCol.push(thisData);
}
}
unFixCol.unshift.apply(unFixCol, fixLeftCol);
unFixCol.push.apply(unFixCol, fixRightCol);
return {
fixLeftCol: fixLeftCol, //固定在左侧
fixRightCol: fixRightCol, //固定在右侧
allCol: unFixCol //固定表头
};
}
},
methods: {
// 固定左侧
tableScroll: function () {
requestAnimationFrame(this.tableScrollMethod);
// this.tableScrollMethod();
},
tableScrollMethod: function () {
// 表头滚动
this.$refs.tableTop && (this.$refs.tableTop.scrollLeft = this.$refs.table.scrollLeft);
// 左侧滚动
this.$refs.tableLeft && (this.$refs.tableLeft.scrollTop = this.$refs.table.scrollTop);
//右侧滚动
this.$refs.tableRight && (this.$refs.tableRight.scrollTop = this.$refs.table.scrollTop);
},
checkFix: function () {
var ths = this.$refs.tableTrue.querySelectorAll('th');
for (var i = 0; i < this.tableColumns.length; i++) {
//设置每个列的宽度
this.$set(this.tableColumnsFix.allCol[i], 'width', ths.item(i).clientWidth);
}
},
//计算表格高度
CalcTableFixHeight: function () {
if (this.$refs.tableTrue.clientHeight <= this.tableHeight) {
//当表格数据不足以充满总高度
this.noLeftScroll = true;
this.tableFixHeight = this.$refs.tableTrue.clientHeight + 'px';
} else {
this.noLeftScroll = false;
this.tableFixHeight = this.tableHeight - 10 + 'px';
}
},
// 双击事件
dblclick: function (rowData, index) {
this.$emit('t-dblclick', rowData, index)
},
// Hover改变事件
trHover: function (index) {
this.nowHover = index;
},
trOut: function () {
this.nowHover = -1;
}
}
})
var app = new Vue({
el: '#app',
data: {
tableHeight: 300,
tableColumns: [{
title: '姓名',
key: 'name',
// fixed: 'left' //固定
}, {
title: '手机',
key: 'phone',
// fixed: 'left'
}, {
title: '年龄',
key: 'age'
}, {
title: '地址1',
key: 'address1',
fixed: 'left'
}, {
title: '地址2',
key: 'address2'
}, {
title: '地址3',
key: 'address3'
}, {
title: '地址4',
key: 'address4'
}, {
title: '地址5',
key: 'address5'
}, {
title: '地址6',
key: 'address6'
}, {
title: '地址7',
key: 'address7',
fixed: 'right'
}, {
title: '地址8',
key: 'address8',
// fixed: 'right'
}],
tableData: [],
},
mounted: function () {
this.getData();
},
methods: {
getData: function () {
// 模拟获取表格数据
var tableData = [];
for (var i = 0; i < 2; i++) {
var data = {
name: 'st' + i,
phone: '15502515852',
age: '22',
address1: '地址1**********************' + i,
address2: '地址2',
address3: '地址3',
address4: '地址4',
address5: '地址5',
address6: '出卖我的爱 逼着我离开 最后知道真相的我眼泪掉下来 出卖我的爱 你背了良心债 就算付出再多感情也再买不回来',
address7: '当初是你要分开 分开就分开',
address8: '现在又要用真爱把我哄回来 '
};
tableData.push(data);
}
setTimeout(() => {
this.tableData = tableData;
}, 500);
},
tableClick: function (data, index) {
console.log(data);
},
}
});
</script>
</html>