StickyTable.vue
4 KB
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
<template>
<div class="sticky-table-container" :style="{ height: height }">
<div class="sticky-table-wrapper" :style="{ height: data.length > 5 ? '100%' : '' }">
<table class="sticky-table">
<thead>
<tr>
<th v-for="(col, index) in columns" v-if="!col.isHidden" :key="index" :class="{ 'fixed-col': index === 0 && fixedFirstColumn }" :style="getHeaderStyle(index)">
{{ col.label }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, rowIndex) in data" :key="rowIndex">
<td v-for="(col, colIndex) in columns" v-if="!col.isHidden" :key="colIndex" :class="{ 'fixed-col': colIndex === 0 && fixedFirstColumn }" :style="getCellStyle(colIndex)">
<div class="cell-content">
<slot :name="col.prop" :row="row" :index="rowIndex">
{{ col.formatter ? col.formatter(row) : row[col.prop] || row[col.prop] === 0 ? row[col.prop] : '-' }}
</slot>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
export default {
name: 'StickyTable',
props: {
// 列定义 [{ label: '名称', prop: 'name', width: '100px' }]
columns: {
type: Array,
required: true
},
// 数据源
data: {
type: Array,
default: () => []
},
// 表格高度
height: {
type: String,
default: 'auto'
},
// 是否固定第一列
fixedFirstColumn: {
type: Boolean,
default: true
}
},
methods: {
getHeaderStyle(index) {
const style = {}
if (this.columns[index].width) {
style.minWidth = this.columns[index].width
style.maxWidth = this.columns[index].width
}
return style
},
getCellStyle(index) {
const style = {}
// 如果有其他样式逻辑可以在这里添加
return style
}
}
}
</script>
<style lang="scss" scoped>
.sticky-table-container {
overflow: hidden;
position: relative;
width: 100%;
background: #fff;
}
.sticky-table-wrapper {
overflow: auto;
// height: 100%;
.sticky-table {
border-collapse: separate; // sticky需要separate或collapse配合特定处理,separate较安全
border-spacing: 0;
min-width: 100%;
th,
td {
box-sizing: border-box;
padding: 0 15px; // 调整为水平内边距
height: 15px;
background: #fff;
text-align: left;
color: #333333;
font-size: 12px;
}
th {
line-height: 15px; // 表头单行垂直居中
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
td {
vertical-align: middle; // 内容垂直居中
}
.cell-content {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
text-overflow: ellipsis;
word-break: break-all;
line-height: 1.4;
white-space: normal;
}
// 最后一列去掉右边框
// th:last-child,
// td:last-child {
// border-right: none;
// }
th:first-child,
td:first-child {
padding: 4px 8px 4px 0;
}
th {
position: sticky;
top: 0;
z-index: 10;
color: #999999;
font-size: 12px;
padding-top: 0;
}
// 固定第一列样式
.fixed-col {
position: sticky;
left: 0;
z-index: 5;
background: #fff; // 必须指定背景色,否则透明
// border-right: 1px solid #ebedf0;
box-shadow: 2px 0 0 0 #adadad1a;
// 添加阴影效果增加层次感
&::after {
content: '';
position: absolute;
top: 0;
right: -4px;
bottom: 0;
width: 4px;
// background: linear-gradient(90deg, rgba(0, 0, 0, 0.05), transparent );
pointer-events: none;
}
}
// 表头和第一列交叉的单元格
th.fixed-col {
padding: 4px 8px 4px 0;
z-index: 15; // 最高层级
background: #fff;
}
}
}
</style>