hy-app 0.5.11 → 0.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,358 +1,579 @@
1
- <template>
2
- <view class="hy-table" :style="{ height: addUnit(height) }">
3
- <!-- 主容器 -->
4
- <view class="hy-table__container">
5
- <!-- 表头区域 -->
6
- <view class="hy-table__header">
7
- <!-- 固定左列表头 -->
8
- <view v-if="leftFixedColumns.length > 0" class="hy-table__header--left">
9
- <view
10
- class="hy-table__header__th"
11
- v-for="(col, index) in leftFixedColumns"
12
- :key="col.key || index"
13
- :style="[getCellStyle(col, true)]"
14
- @click="handleSort(col)"
15
- >
16
- <text class="hy-table__header__th--text">{{ col.title }}</text>
17
- <!-- 排序图标 -->
18
- <view v-if="col.sortable" class="hy-table__header__th--sort">
19
- <hy-icon
20
- :name="IconConfig.ARROW_UP_FILL"
21
- size="10"
22
- :custom-class="sortKey === col.key && sortType === 'asc' ? 'is-active' : ''"
23
- />
24
- <hy-icon
25
- :name="IconConfig.ARROW_DOWN_FILL"
26
- size="10"
27
- :custom-class="sortKey === col.key && sortType === 'desc' ? 'is-active' : ''"
28
- />
29
- </view>
30
- </view>
31
- </view>
32
- <!-- 中间表头 -->
33
- <view class="hy-table__header--center" :style="{ paddingLeft: `${leftFixedWidth}px`, paddingRight: `${rightFixedWidth}px` }">
34
- <view
35
- class="hy-table__header__th"
36
- v-for="(col, index) in scrollColumns"
37
- :key="col.key || index"
38
- :style="[getCellStyle(col, true)]"
39
- @click="handleSort(col)"
40
- >
41
- <text class="hy-table__header__th--text">{{ col.title }}</text>
42
- <!-- 排序图标 -->
43
- <view v-if="col.sortable" class="hy-table__header__th--sort">
44
- <hy-icon
45
- :name="IconConfig.ARROW_UP_FILL"
46
- size="10"
47
- :custom-class="sortKey === col.key && sortType === 'asc' ? 'is-active' : ''"
48
- />
49
- <hy-icon
50
- :name="IconConfig.ARROW_DOWN_FILL"
51
- size="10"
52
- :custom-class="sortKey === col.key && sortType === 'desc' ? 'is-active' : ''"
53
- />
54
- </view>
55
- </view>
56
- </view>
57
- <!-- 固定右列表头 -->
58
- <view v-if="rightFixedColumns.length > 0" class="hy-table__header--right">
59
- <view
60
- class="hy-table__header__th"
61
- v-for="(col, index) in rightFixedColumns"
62
- :key="col.key || index"
63
- :style="[getCellStyle(col, true)]"
64
- @click="handleSort(col)"
65
- >
66
- <text class="hy-table__header__th--text">{{ col.title }}</text>
67
- <!-- 排序图标 -->
68
- <view v-if="col.sortable" class="hy-table__header__th--sort">
69
- <hy-icon
70
- :name="IconConfig.ARROW_UP_FILL"
71
- size="10"
72
- :custom-class="sortKey === col.key && sortType === 'asc' ? 'is-active' : ''"
73
- />
74
- <hy-icon
75
- :name="IconConfig.ARROW_DOWN_FILL"
76
- size="10"
77
- :custom-class="sortKey === col.key && sortType === 'desc' ? 'is-active' : ''"
78
- />
79
- </view>
80
- </view>
81
- </view>
82
- </view>
83
-
84
- <!-- 内容区域 -->
85
- <scroll-view
86
- class="hy-table__body"
87
- scroll-y
88
- :scroll-top="scrollTop"
89
- @scroll="handleScroll"
90
- >
91
- <!-- 内容包装器 -->
92
- <view class="hy-table__body__wrapper">
93
- <!-- 固定左列内容 -->
94
- <view v-if="leftFixedColumns.length > 0" class="hy-table__body--left">
95
- <view
96
- class="hy-table__body__tr"
97
- v-for="(row, rowIndex) in tableData"
98
- :key="`left-${row[rowKey] || rowIndex}`"
99
- >
100
- <view
101
- class="hy-table__body__td"
102
- v-for="(col, colIndex) in leftFixedColumns"
103
- :key="col.key || colIndex"
104
- :style="[getCellStyle(col, false)]"
105
- @click.stop="handleCellClick(row, col)"
106
- >
107
- <slot name="default" :row="row" :col="col" :index="rowIndex">
108
- <text :class="{ 'hy-table__body__td--ellipsis': col.ellipsis }">{{ row[col.key] }}</text>
109
- </slot>
110
- </view>
111
- </view>
112
- </view>
113
- <!-- 中间内容 -->
114
- <view class="hy-table__body--center" :style="{ paddingLeft: `${leftFixedWidth}px`, paddingRight: `${rightFixedWidth}px` }">
115
- <view
116
- class="hy-table__body__tr"
117
- v-for="(row, rowIndex) in tableData"
118
- :key="row[rowKey] || rowIndex"
119
- @click="handleRowClick(row, rowIndex)"
120
- >
121
- <view
122
- class="hy-table__body__td"
123
- v-for="(col, colIndex) in scrollColumns"
124
- :key="col.key || colIndex"
125
- :style="[getCellStyle(col, false)]"
126
- @click.stop="handleCellClick(row, col)"
127
- >
128
- <slot name="default" :row="row" :col="col" :index="rowIndex">
129
- <text :class="{ 'hy-table__body__td--ellipsis': col.ellipsis }">{{ row[col.key] }}</text>
130
- </slot>
131
- </view>
132
- </view>
133
- </view>
134
- <!-- 固定右列内容 -->
135
- <view v-if="rightFixedColumns.length > 0" class="hy-table__body--right">
136
- <view
137
- class="hy-table__body__tr"
138
- v-for="(row, rowIndex) in tableData"
139
- :key="`right-${row[rowKey] || rowIndex}`"
140
- >
141
- <view
142
- class="hy-table__body__td"
143
- v-for="(col, colIndex) in rightFixedColumns"
144
- :key="col.key || colIndex"
145
- :style="[getCellStyle(col, false)]"
146
- @click.stop="handleCellClick(row, col)"
147
- >
148
- <slot name="default" :row="row" :col="col" :index="rowIndex">
149
- <text :class="{ 'hy-table__body__td--ellipsis': col.ellipsis }">{{ row[col.key] }}</text>
150
- </slot>
151
- </view>
152
- </view>
153
- </view>
154
- </view>
155
-
156
- <!-- 空状态 -->
157
- <hy-empty
158
- v-if="!loading && tableData.length === 0"
159
- :description="emptyText"
160
- ></hy-empty>
161
-
162
- <!-- 加载状态 -->
163
- <hy-loading
164
- v-if="loading"
165
- text="加载中..."
166
- mode="circle"
167
- direction="column"
168
- custom-class="hy-table__body--loading"
169
- ></hy-loading>
170
- </scroll-view>
171
- </view>
172
- </view>
173
- </template>
174
-
175
- <script lang="ts">
176
- export default {
177
- name: 'hy-table',
178
- options: {
179
- addGlobalClass: true,
180
- virtualHost: true,
181
- styleIsolation: 'shared'
182
- }
183
- }
184
- </script>
185
-
186
- <script setup lang="ts">
187
- import { computed, ref, watch } from 'vue'
188
- import tableProps from './props'
189
- import type { ITableColumn, ITableEmits, SortType } from './typing'
190
- import { addUnit, IconConfig } from '../../libs'
191
- // 组件
192
- import HyEmpty from '../hy-empty/hy-empty.vue'
193
- import HyLoading from '../hy-loading/hy-loading.vue'
194
- import HyIcon from '../hy-icon/hy-icon.vue'
195
-
196
- /**
197
- * 用于展示多条结构类似的数据, 可对数据进行排序等操作。
198
- * @displayName hy-table
199
- */
200
- defineOptions({})
201
-
202
- const props = defineProps(tableProps)
203
- const emit = defineEmits<ITableEmits>()
204
-
205
- // --- 状态管理 ---
206
- const sortKey = ref<string>('')
207
- const sortType = ref<SortType>('normal')
208
- const tableData = ref<any[]>([]) // 内部维护的数据,用于排序展示
209
- const scrollTop = ref<number>(0) // 滚动位置,用于控制表头定位
210
-
211
- // --- 监听数据变化 ---
212
- watch(
213
- () => props.data,
214
- (val) => {
215
- tableData.value = [...val]
216
- // 如果当前有排序状态,重新应用排序
217
- if (sortKey.value && sortType.value) {
218
- localSort()
219
- }
220
- },
221
- { immediate: true, deep: true }
222
- )
223
-
224
- // --- 计算固定列和滚动列 ---
225
- const leftFixedColumns = computed(() => {
226
- return props.columns.filter(col => col.fixed === 'left')
227
- })
228
-
229
- const rightFixedColumns = computed(() => {
230
- return props.columns.filter(col => col.fixed === 'right')
231
- })
232
-
233
- const scrollColumns = computed(() => {
234
- return props.columns.filter(col => !col.fixed)
235
- })
236
-
237
- // --- 计算固定列总宽度 ---
238
- const leftFixedWidth = computed(() => {
239
- let width = 0
240
- leftFixedColumns.value.forEach(col => {
241
- const w = parseInt(String(col.width || 100))
242
- width += w
243
- })
244
- return width
245
- })
246
-
247
- const rightFixedWidth = computed(() => {
248
- let width = 0
249
- rightFixedColumns.value.forEach(col => {
250
- const w = parseInt(String(col.width || 100))
251
- width += w
252
- })
253
- return width
254
- })
255
-
256
- // --- 计算总宽度 (用于 scroll-view 横向滚动) ---
257
- const totalMinWidth = computed(() => {
258
- let width = 0
259
- scrollColumns.value.forEach((col) => {
260
- // 简单估算:如果有明确宽度则累加,否则给个默认值 100
261
- const w = parseInt(String(col.width || 100))
262
- width += w
263
- })
264
- return width
265
- })
266
-
267
- // --- 事件处理 ---
268
- // 处理滚动事件
269
- const handleScroll = (e: any) => {
270
- scrollTop.value = e.detail.scrollTop
271
- }
272
-
273
- // --- 样式计算 ---
274
- const getCellStyle = (col: ITableColumn, isHeader: boolean) => {
275
- const style: any = {}
276
-
277
- // 处理宽度
278
- if (col.width) {
279
- const w = typeof col.width === 'number' ? `${col.width}px` : col.width
280
- style.width = w
281
- style.flex = `0 0 ${w}` // 固定宽度,不伸缩
282
- } else {
283
- style.flex = 1 // 自动填充
284
- style.minWidth = '150rpx' // 最小宽度防止太挤
285
- }
286
-
287
- // 处理对齐
288
- style.justifyContent =
289
- col.align === 'center' ? 'center' : col.align === 'right' ? 'flex-end' : 'flex-start'
290
- style.textAlign = col.align || 'left'
291
-
292
- return style
293
- }
294
-
295
- // --- 排序逻辑 ---
296
- const handleSort = (col: ITableColumn) => {
297
- if (!col.sortable) return
298
-
299
- // 切换排序状态: '' -> 'asc' -> 'desc' -> ''
300
- if (sortKey.value !== col.key) {
301
- sortKey.value = col.key
302
- sortType.value = 'asc'
303
- } else {
304
- if (sortType.value === 'asc') sortType.value = 'desc'
305
- else if (sortType.value === 'desc') sortType.value = 'normal'
306
- else sortType.value = 'asc' // 逻辑闭环
307
- }
308
-
309
- // 触发本地排序或远程排序事件
310
- localSort()
311
- emit('sort-change', { key: sortKey.value, type: sortType.value })
312
- }
313
-
314
- const localSort = () => {
315
- if (!sortType.value) {
316
- tableData.value = [...props.data] // 还原
317
- return
318
- }
319
-
320
- tableData.value.sort((a, b) => {
321
- const valA = a[sortKey.value]
322
- const valB = b[sortKey.value]
323
-
324
- // 简单的数字和字符串比较
325
- if (valA === valB) return 0
326
-
327
- let result = 0
328
- if (typeof valA === 'number' && typeof valB === 'number') {
329
- result = valA - valB
330
- } else {
331
- result = String(valA).localeCompare(String(valB))
332
- }
333
-
334
- return sortType.value === 'asc' ? result : -result
335
- })
336
- }
337
-
338
- // --- 交互逻辑 ---
339
- const handleRowClick = (row: any, index: number) => {
340
- emit('row-click', row, index)
341
- }
342
-
343
- // 处理单元格点击(主要用于Tooltip)
344
- const handleCellClick = (row: any, col: ITableColumn) => {
345
- // 如果开启了 tooltip,显示完整内容
346
- if (col.tooltip && col.ellipsis) {
347
- const content = row[col.key]
348
- if (content) {
349
- uni.showToast({ title: String(content), icon: 'none' })
350
- }
351
- }
352
- emit('cell-click', row, col)
353
- }
354
- </script>
355
-
356
- <style lang="scss" scoped>
357
- @import './index.scss';
358
- </style>
1
+ <template>
2
+ <view class="hy-table" :style="{ height: addUnit(containerHeight) }">
3
+ <!-- 列头 -->
4
+ <view class="hy-table__header" v-if="showHeader">
5
+ <scroll-view
6
+ class="hy-table__header--scroll"
7
+ scroll-x
8
+ :scroll-left="scrollLeft"
9
+ :scroll-y="false"
10
+ @scroll="onHeaderScroll"
11
+ >
12
+ <view class="hy-table__header--wrapper" :style="{ width: addUnit(totalWidth) }">
13
+ <!-- 左侧固定列头 -->
14
+ <view
15
+ v-if="leftFixedColumns.length > 0"
16
+ :class="['hy-table__header--wrapper__left', scrollLeft && 'is-shadow']"
17
+ :style="{ width: addUnit(leftFixedWidth), zIndex: 3 }"
18
+ >
19
+ <view
20
+ v-for="(col, colIndex) in leftFixedColumns"
21
+ :key="colIndex"
22
+ class="hy-table__header--wrapper__cell"
23
+ :style="getHeaderCellStyle(col)"
24
+ @tap="handleSort(col)"
25
+ >
26
+ <slot v-if="$slots['left-head']" name="left-head" :col="col">
27
+ <text class="header-text">{{ col.title }}</text>
28
+ </slot>
29
+ <text v-else class="hy-table__header--wrapper__cell--text">{{
30
+ col.title
31
+ }}</text>
32
+ <view
33
+ v-if="col.sortable"
34
+ class="hy-table__header--wrapper__cell--sortable"
35
+ >
36
+ <hy-icon
37
+ :name="IconConfig.ARROW_UP_FILL"
38
+ size="12"
39
+ :custom-class="
40
+ sortField === col.key && sortOrder === 'asc'
41
+ ? 'is-active'
42
+ : ''
43
+ "
44
+ @click.stop="handleSort(col, 'asc')"
45
+ ></hy-icon>
46
+
47
+ <hy-icon
48
+ :name="IconConfig.ARROW_DOWN_FILL"
49
+ size="12"
50
+ :custom-class="
51
+ sortField === col.key && sortOrder === 'desc'
52
+ ? 'is-active'
53
+ : ''
54
+ "
55
+ @click.stop="handleSort(col, 'desc')"
56
+ ></hy-icon>
57
+ </view>
58
+ </view>
59
+ </view>
60
+
61
+ <!-- 中间滚动列头 -->
62
+ <view class="hy-table__header--wrapper__center">
63
+ <view
64
+ v-for="(col, colIndex) in scrollColumns"
65
+ :key="colIndex"
66
+ class="hy-table__header--wrapper__cell"
67
+ :style="getHeaderCellStyle(col)"
68
+ @tap="handleSort(col)"
69
+ >
70
+ <slot v-if="$slots.head" name="head" :col="col"></slot>
71
+ <text v-else class="hy-table__header--wrapper__cell--text">{{
72
+ col.title
73
+ }}</text>
74
+ <view
75
+ v-if="col.sortable"
76
+ class="hy-table__header--wrapper__cell--sortable"
77
+ >
78
+ <hy-icon
79
+ :name="IconConfig.ARROW_UP_FILL"
80
+ size="12"
81
+ :custom-class="
82
+ sortField === col.key && sortOrder === 'asc'
83
+ ? 'is-active'
84
+ : ''
85
+ "
86
+ @click.stop="handleSort(col, 'asc')"
87
+ ></hy-icon>
88
+
89
+ <hy-icon
90
+ :name="IconConfig.ARROW_DOWN_FILL"
91
+ size="12"
92
+ :custom-class="
93
+ sortField === col.key && sortOrder === 'desc'
94
+ ? 'is-active'
95
+ : ''
96
+ "
97
+ @click.stop="handleSort(col, 'desc')"
98
+ ></hy-icon>
99
+ </view>
100
+ </view>
101
+ </view>
102
+
103
+ <!-- 右侧固定列头 -->
104
+ <view
105
+ v-if="rightFixedColumns.length > 0"
106
+ class="hy-table__header--wrapper__right"
107
+ :style="{ width: addUnit(rightFixedWidth), zIndex: 3 }"
108
+ >
109
+ <view
110
+ v-for="(col, colIndex) in rightFixedColumns"
111
+ :key="colIndex"
112
+ class="hy-table__header--wrapper__cell"
113
+ :style="getHeaderCellStyle(col)"
114
+ @tap="handleSort(col)"
115
+ >
116
+ <slot v-if="$slots['right-head']" name="right-head" :col="col"></slot>
117
+ <text v-else class="hy-table__header--wrapper__cell--text">{{
118
+ col.title
119
+ }}</text>
120
+ <view
121
+ v-if="col.sortable"
122
+ class="hy-table__header--wrapper__cell--sortable"
123
+ >
124
+ <hy-icon
125
+ :name="IconConfig.ARROW_UP_FILL"
126
+ size="12"
127
+ :custom-class="
128
+ sortField === col.key && sortOrder === 'asc'
129
+ ? 'is-active'
130
+ : ''
131
+ "
132
+ @click.stop="handleSort(col, 'asc')"
133
+ ></hy-icon>
134
+
135
+ <hy-icon
136
+ :name="IconConfig.ARROW_DOWN_FILL"
137
+ size="12"
138
+ :custom-class="
139
+ sortField === col.key && sortOrder === 'desc'
140
+ ? 'is-active'
141
+ : ''
142
+ "
143
+ @click.stop="handleSort(col, 'desc')"
144
+ ></hy-icon>
145
+ </view>
146
+ </view>
147
+ </view>
148
+ </view>
149
+ </scroll-view>
150
+ </view>
151
+
152
+ <!-- 表格主体 -->
153
+ <view class="hy-table__body">
154
+ <view v-if="loading" class="hy-table__body--loading">
155
+ <hy-loading text="加载中..." mode="circle"></hy-loading>
156
+ </view>
157
+ <view v-if="!data.length" class="hy-table__body--empty">
158
+ <slot v-if="$slots.empty" name="empty"></slot>
159
+ <hy-empty v-else :image-url="emptyUrl" :description="emptyDes"></hy-empty>
160
+ </view>
161
+ <!-- 左侧固定列 -->
162
+ <scroll-view
163
+ v-if="processedData.length"
164
+ :class="['hy-table__body--left', scrollLeft && 'is-shadow']"
165
+ scroll-y
166
+ :scroll-top="scrollTop"
167
+ :style="{ width: addUnit(leftFixedWidth), height: addUnit(bodyHeight) }"
168
+ @scroll="onLeftScroll"
169
+ >
170
+ <view class="hy-table__body--content">
171
+ <view
172
+ v-for="(row, rowIndex) in processedData"
173
+ :key="rowIndex"
174
+ :class="rowClass(rowIndex)"
175
+ :style="getRowStyle(rowIndex)"
176
+ >
177
+ <view
178
+ v-for="(col, colIndex) in leftFixedColumns"
179
+ :key="colIndex"
180
+ class="hy-table__body--content__row--cell"
181
+ :style="getBodyCellStyle(col, rowIndex)"
182
+ @tap="onCellClick(row, rowIndex)"
183
+ >
184
+ <slot
185
+ v-if="$slots.left"
186
+ name="left"
187
+ :row="row"
188
+ :col="col"
189
+ :index="rowIndex"
190
+ ></slot>
191
+ <text
192
+ v-else
193
+ class="hy-table__body--content__row--cell__text"
194
+ :class="{ 'is-ellipsis': col.ellipsis }"
195
+ >
196
+ {{ getCellValue(row, col) }}
197
+ </text>
198
+ </view>
199
+ </view>
200
+ </view>
201
+ </scroll-view>
202
+
203
+ <!-- 中间滚动区域 -->
204
+ <scroll-view
205
+ v-if="processedData.length"
206
+ class="hy-table__body--center"
207
+ scroll-y
208
+ :scroll-top="scrollTop"
209
+ @scroll="onScroll"
210
+ :style="{
211
+ width: `calc(100% - ${leftFixedWidth + rightFixedWidth}px)`,
212
+ height: addUnit(bodyHeight),
213
+ left: addUnit(leftFixedWidth),
214
+ right: addUnit(rightFixedWidth)
215
+ }"
216
+ >
217
+ <scroll-view
218
+ scroll-x
219
+ :scroll-left="scrollLeft"
220
+ class="hy-table__body--content"
221
+ @scroll="onCrosswiseScroll"
222
+ >
223
+ <view
224
+ v-for="(row, rowIndex) in processedData"
225
+ :key="rowIndex"
226
+ :class="rowClass(rowIndex)"
227
+ :style="getRowStyle(rowIndex)"
228
+ >
229
+ <view
230
+ v-for="(col, colIndex) in scrollColumns"
231
+ :key="colIndex"
232
+ class="hy-table__body--content__row--cell"
233
+ :class="{ ellipsis: col.ellipsis }"
234
+ :style="getBodyCellStyle(col, rowIndex)"
235
+ @tap="onCellClick(row, rowIndex)"
236
+ >
237
+ <slot
238
+ v-if="$slots.default"
239
+ name="default"
240
+ :row="row"
241
+ :col="col"
242
+ :index="rowIndex"
243
+ ></slot>
244
+ <text
245
+ v-else
246
+ class="hy-table__body--content__row--cell__text"
247
+ :class="{ 'is-ellipsis': col.ellipsis }"
248
+ >
249
+ {{ getCellValue(row, col) }}
250
+ </text>
251
+ </view>
252
+ </view>
253
+ </scroll-view>
254
+ </scroll-view>
255
+
256
+ <!-- 右侧固定列 -->
257
+ <scroll-view
258
+ v-if="processedData.length"
259
+ class="hy-table__body--right"
260
+ scroll-y
261
+ :scroll-top="scrollTop"
262
+ :style="{
263
+ width: addUnit(rightFixedWidth),
264
+ height: addUnit(bodyHeight),
265
+ right: '0'
266
+ }"
267
+ @scroll="onRightScroll"
268
+ >
269
+ <view class="hy-table__body--content">
270
+ <view
271
+ v-for="(row, rowIndex) in processedData"
272
+ :key="rowIndex"
273
+ :class="rowClass(rowIndex)"
274
+ :style="getRowStyle(rowIndex)"
275
+ >
276
+ <view
277
+ v-for="(col, colIndex) in rightFixedColumns"
278
+ :key="colIndex"
279
+ class="hy-table__body--content__row--cell"
280
+ :style="getBodyCellStyle(col, rowIndex)"
281
+ @tap="onCellClick(row, rowIndex)"
282
+ >
283
+ <slot
284
+ v-if="$slots.right"
285
+ name="right"
286
+ :row="row"
287
+ :col="col"
288
+ :index="rowIndex"
289
+ ></slot>
290
+ <text
291
+ v-else
292
+ class="hy-table__body--content__row--cell__text"
293
+ :class="{ 'is-ellipsis': col.ellipsis }"
294
+ >
295
+ {{ getCellValue(row, col) }}
296
+ </text>
297
+ </view>
298
+ </view>
299
+ </view>
300
+ </scroll-view>
301
+ </view>
302
+ </view>
303
+ </template>
304
+
305
+ <script setup lang="ts">
306
+ import { ref, computed, watch, onMounted } from 'vue'
307
+ import { addUnit, IconConfig, sleep, getPx } from '../../libs'
308
+ import type { ITableColumn, ITableEmits } from './typing'
309
+ import tableProps from './props'
310
+ // 组件
311
+ import HyIcon from '../hy-icon/hy-icon.vue'
312
+ import HyEmpty from '../hy-empty/hy-empty.vue'
313
+ import HyLoading from '../hy-loading/hy-loading.vue'
314
+
315
+ const props = defineProps(tableProps)
316
+ const emit = defineEmits<ITableEmits>()
317
+
318
+ // 响应式数据
319
+ const scrollLeft = ref(0)
320
+ const scrollTop = ref(0)
321
+ const rowHeights = ref<number[]>([])
322
+ const sortField = ref<string>('')
323
+ const sortOrder = ref<'asc' | 'desc'>('asc')
324
+
325
+ // 防止滚动循环触发和抖动的标志位
326
+ const isUpdatingScroll = ref(false)
327
+
328
+ // 计算属性
329
+ const leftFixedColumns = computed(() => props.columns.filter((col) => col.fixed === 'left'))
330
+
331
+ const rightFixedColumns = computed(() => props.columns.filter((col) => col.fixed === 'right'))
332
+
333
+ const scrollColumns = computed(() => props.columns.filter((col) => !col?.fixed))
334
+
335
+ const leftFixedWidth = computed(() =>
336
+ leftFixedColumns.value.reduce((sum, col) => sum + col.width, 0)
337
+ )
338
+
339
+ const rightFixedWidth = computed(() =>
340
+ rightFixedColumns.value.reduce((sum, col) => sum + col.width, 0)
341
+ )
342
+
343
+ const scrollWidth = computed(() => scrollColumns.value.reduce((sum, col) => sum + col.width, 0))
344
+
345
+ const totalWidth = computed(() => leftFixedWidth.value + scrollWidth.value + rightFixedWidth.value)
346
+
347
+ const containerHeight = computed(() => props.height)
348
+
349
+ const bodyHeight = computed(() => {
350
+ // 减去表头高度
351
+ return props.showHeader ? getPx(props.height) - 50 : props.height
352
+ })
353
+
354
+ const processedData = computed(() => {
355
+ let data = [...props.data]
356
+
357
+ if (sortField.value) {
358
+ data.sort((a, b) => {
359
+ const aVal = a[sortField.value]
360
+ const bVal = b[sortField.value]
361
+
362
+ if (aVal === bVal) return 0
363
+ if (sortOrder.value === 'asc') {
364
+ return aVal > bVal ? 1 : -1
365
+ } else {
366
+ return aVal < bVal ? 1 : -1
367
+ }
368
+ })
369
+ }
370
+
371
+ return data
372
+ })
373
+
374
+ // 方法
375
+ const getHeaderCellStyle = (col: ITableColumn) => {
376
+ return {
377
+ width: addUnit(col.width),
378
+ textAlign: col.align || 'left',
379
+ flexShrink: 0
380
+ }
381
+ }
382
+
383
+ const getBodyCellStyle = (col: ITableColumn, rowIndex: number) => {
384
+ const rowHeight = rowHeights.value[rowIndex] || props.rowHeight
385
+ return {
386
+ width: addUnit(col.width),
387
+ textAlign: col.align || 'left',
388
+ flexShrink: 0,
389
+ height: addUnit(rowHeight),
390
+ lineHeight: addUnit(rowHeight)
391
+ }
392
+ }
393
+
394
+ /**
395
+ * 行类名
396
+ * */
397
+ const rowClass = computed(() => {
398
+ return (rowIndex: number) => {
399
+ const classes = ['hy-table__body--content__row']
400
+
401
+ if (props.stripe && rowIndex % 2 === 1) {
402
+ classes.push('is-stripe')
403
+ }
404
+
405
+ return classes
406
+ }
407
+ })
408
+ /**
409
+ * 行样式
410
+ * */
411
+ const getRowStyle = computed(() => {
412
+ return (rowIndex: number) => {
413
+ const rowHeight = rowHeights.value[rowIndex] || props.rowHeight
414
+ const styles: any = {
415
+ width: addUnit(scrollWidth.value),
416
+ height: addUnit(rowHeight),
417
+ lineHeight: addUnit(rowHeight)
418
+ }
419
+
420
+ return styles
421
+ }
422
+ })
423
+
424
+ const getCellValue = (row: any, col: ITableColumn) => {
425
+ if (col.formatter) {
426
+ return col.formatter(row[col.key], row)
427
+ }
428
+ return row[col.key] || ''
429
+ }
430
+
431
+ const onHeaderScroll = async (e: any) => {
432
+ if (isUpdatingScroll.value) return
433
+
434
+ const newScrollLeft = e.detail.scrollLeft
435
+ if (Math.abs(newScrollLeft - scrollLeft.value) > 1) {
436
+ isUpdatingScroll.value = true
437
+ scrollLeft.value = newScrollLeft
438
+ // 在下一个事件循环重置标志位
439
+ await sleep(0)
440
+ isUpdatingScroll.value = false
441
+ }
442
+ }
443
+
444
+ /**
445
+ * 中间内容竖直滚动
446
+ * */
447
+ const onScroll = async (e: any) => {
448
+ if (isUpdatingScroll.value) return
449
+
450
+ const newScrollTop = e.detail.scrollTop
451
+ isUpdatingScroll.value = true
452
+ if (Math.abs(newScrollTop - scrollTop.value) > 1) {
453
+ scrollTop.value = newScrollTop
454
+ }
455
+
456
+ // 在下一个事件循环重置标志位
457
+ await sleep(0)
458
+ isUpdatingScroll.value = false
459
+ }
460
+
461
+ /**
462
+ * 中间内容横向滚动
463
+ * */
464
+ const onCrosswiseScroll = async (e: any) => {
465
+ if (isUpdatingScroll.value) return
466
+
467
+ const newScrollLeft = e.detail.scrollLeft
468
+ isUpdatingScroll.value = true
469
+ // 使用阈值来减少频繁更新,避免抖动
470
+ if (Math.abs(newScrollLeft - scrollLeft.value) > 1) {
471
+ scrollLeft.value = newScrollLeft
472
+ }
473
+ // 在下一个事件循环重置标志位
474
+ await sleep(0)
475
+ isUpdatingScroll.value = false
476
+ }
477
+
478
+ /**
479
+ * 左侧列表滚动
480
+ * */
481
+ const onLeftScroll = async (e: any) => {
482
+ if (isUpdatingScroll.value) return
483
+
484
+ const newScrollTop = e.detail.scrollTop
485
+ if (Math.abs(newScrollTop - scrollTop.value) > 1) {
486
+ isUpdatingScroll.value = true
487
+ scrollTop.value = newScrollTop
488
+ // 在下一个事件循环重置标志位
489
+ await sleep(0)
490
+ isUpdatingScroll.value = false
491
+ }
492
+ }
493
+
494
+ /**
495
+ * 右侧列表滚动
496
+ * */
497
+ const onRightScroll = async (e: any) => {
498
+ if (isUpdatingScroll.value) return
499
+
500
+ const newScrollTop = e.detail.scrollTop
501
+ if (Math.abs(newScrollTop - scrollTop.value) > 1) {
502
+ isUpdatingScroll.value = true
503
+ scrollTop.value = newScrollTop
504
+ // 在下一个事件循环重置标志位
505
+ await sleep(0)
506
+ isUpdatingScroll.value = false
507
+ }
508
+ }
509
+
510
+ /**
511
+ * 点击排序
512
+ * @param col 列标题
513
+ * @param sort 排序
514
+ * */
515
+ const handleSort = (col: ITableColumn, sort?: 'asc' | 'desc') => {
516
+ if (!col.sortable) return
517
+
518
+ if (sort) {
519
+ sortOrder.value = sort
520
+ sortField.value = col.key
521
+ } else {
522
+ if (sortField.value === col.key) {
523
+ sortOrder.value = sortOrder.value === 'asc' ? 'desc' : 'asc'
524
+ } else {
525
+ sortField.value = col.key
526
+ sortOrder.value = 'asc'
527
+ }
528
+ }
529
+
530
+ emit('sort-change', sortField.value, sortOrder.value)
531
+ }
532
+
533
+ /**
534
+ * 点击单元格
535
+ * @param row 行数据
536
+ * @param index 第几列
537
+ * */
538
+ const onCellClick = (row: AnyObject, index: number) => {
539
+ emit('row-click', row, index)
540
+ }
541
+
542
+ // 初始化行高
543
+ onMounted(() => {
544
+ rowHeights.value = new Array(props.data.length).fill(props.rowHeight)
545
+ })
546
+
547
+ // 监听数据变化
548
+ watch(
549
+ () => props.data,
550
+ () => {
551
+ rowHeights.value = new Array(props.data.length).fill(props.rowHeight)
552
+ },
553
+ { deep: true }
554
+ )
555
+ </script>
556
+
557
+ <style scoped lang="scss">
558
+ @import './index.scss';
559
+
560
+ .header-text {
561
+ white-space: nowrap;
562
+ }
563
+
564
+ .sort-icons {
565
+ display: flex;
566
+ flex-direction: column;
567
+ margin-left: 4px;
568
+ }
569
+
570
+ .sort-icon {
571
+ font-size: 10px;
572
+ color: #ccc;
573
+ line-height: 0.8;
574
+ }
575
+
576
+ .sort-icon.active {
577
+ color: #1890ff;
578
+ }
579
+ </style>