@idooel/components 0.0.2-beta.3 → 0.0.2-beta.31

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.
Files changed (113) hide show
  1. package/README.md +98 -98
  2. package/dist/@idooel/components.esm.js +3599 -1574
  3. package/dist/@idooel/components.umd.js +3653 -1629
  4. package/jsconfig.json +7 -7
  5. package/package.json +64 -50
  6. package/packages/alert/index.js +4 -4
  7. package/packages/alert/src/index.vue +45 -45
  8. package/packages/batch-export/index.js +4 -4
  9. package/packages/batch-export/src/index.vue +104 -104
  10. package/packages/business-components/modal-fsm/index.js +4 -4
  11. package/packages/business-components/modal-fsm/src/index.vue +163 -163
  12. package/packages/business-components/modal-import/index.js +4 -4
  13. package/packages/business-components/modal-import/src/index.vue +222 -139
  14. package/packages/business-components/modal-timeline/index.js +4 -4
  15. package/packages/business-components/modal-timeline/src/index.vue +77 -77
  16. package/packages/business-components/tabs-sub-center/index.js +4 -4
  17. package/packages/business-components/tabs-sub-center/src/index.vue +116 -116
  18. package/packages/button/index.js +4 -4
  19. package/packages/button/src/index.vue +65 -65
  20. package/packages/checkbox/index.js +4 -4
  21. package/packages/checkbox/src/index.vue +52 -52
  22. package/packages/composite-components/button-group/index.js +4 -4
  23. package/packages/composite-components/button-group/src/index.vue +151 -151
  24. package/packages/composite-components/form-attachment/src/index.vue +14 -14
  25. package/packages/composite-components/form-img-crop/index.js +4 -4
  26. package/packages/composite-components/form-img-crop/src/index.vue +131 -131
  27. package/packages/composite-components/modal-confirm/index.js +4 -4
  28. package/packages/composite-components/modal-confirm/src/index.vue +103 -103
  29. package/packages/composite-components/modal-form/index.js +4 -4
  30. package/packages/composite-components/modal-form/src/index.vue +230 -230
  31. package/packages/composite-components/modal-img-crop/index.js +4 -4
  32. package/packages/composite-components/modal-img-crop/src/index.vue +298 -298
  33. package/packages/composite-components/modal-table/index.js +4 -4
  34. package/packages/composite-components/modal-table/src/index.vue +155 -155
  35. package/packages/composite-components/modal-tree/index.js +4 -4
  36. package/packages/composite-components/modal-tree/src/index.vue +75 -75
  37. package/packages/composite-components/search-area/index.js +4 -4
  38. package/packages/composite-components/search-area/src/index.vue +239 -237
  39. package/packages/composite-components/search-area/src/label.vue +35 -35
  40. package/packages/composite-components/select-entity-modal-table/index.js +4 -4
  41. package/packages/composite-components/select-entity-modal-table/src/index.vue +171 -171
  42. package/packages/date/index.js +4 -4
  43. package/packages/date/src/index.vue +112 -112
  44. package/packages/date-range/index.js +4 -4
  45. package/packages/date-range/src/index.vue +47 -47
  46. package/packages/form/index.js +4 -4
  47. package/packages/form/src/index.vue +393 -319
  48. package/packages/icon/index.js +4 -4
  49. package/packages/icon/src/index.vue +31 -31
  50. package/packages/index.js +159 -153
  51. package/packages/input/index.js +4 -4
  52. package/packages/input/src/index.vue +35 -35
  53. package/packages/input-number/index.js +4 -4
  54. package/packages/input-number/src/index.vue +23 -23
  55. package/packages/loading/index.js +4 -4
  56. package/packages/loading/src/index.vue +36 -36
  57. package/packages/meta/provider.js +4 -0
  58. package/packages/modal/index.js +4 -4
  59. package/packages/modal/src/index.vue +184 -184
  60. package/packages/models/form-group-model/index.js +4 -4
  61. package/packages/models/form-group-model/src/index.vue +271 -273
  62. package/packages/models/form-model/index.js +4 -4
  63. package/packages/models/form-model/src/index.vue +236 -232
  64. package/packages/models/step-model/index.js +4 -4
  65. package/packages/models/step-model/src/index.vue +224 -224
  66. package/packages/models/tree-table-model/README.md +0 -0
  67. package/packages/models/tree-table-model/index.js +4 -4
  68. package/packages/models/tree-table-model/src/index.vue +963 -689
  69. package/packages/pagination/index.js +5 -0
  70. package/packages/pagination/src/index.vue +372 -0
  71. package/packages/radio/index.js +4 -4
  72. package/packages/radio/src/index.vue +56 -56
  73. package/packages/select/index.js +4 -4
  74. package/packages/select/src/index.vue +113 -105
  75. package/packages/select-entity/index.js +4 -4
  76. package/packages/select-entity/src/index.vue +119 -119
  77. package/packages/table/index.js +4 -4
  78. package/packages/table/src/action.vue +176 -172
  79. package/packages/table/src/index.vue +605 -319
  80. package/packages/tabs/index.js +4 -4
  81. package/packages/tabs/src/index.vue +55 -55
  82. package/packages/text/index.js +4 -4
  83. package/packages/text/src/index.vue +47 -47
  84. package/packages/text-editor/index.js +4 -4
  85. package/packages/text-editor/src/index.vue +72 -72
  86. package/packages/textarea/index.js +4 -4
  87. package/packages/textarea/src/index.vue +57 -57
  88. package/packages/theme/form.scss +21 -21
  89. package/packages/theme/index.scss +43 -43
  90. package/packages/theme/overrid.scss +7 -7
  91. package/packages/theme/styleClass.scss +2 -2
  92. package/packages/theme/variables.scss +55 -55
  93. package/packages/timeline/index.js +4 -4
  94. package/packages/timeline/src/index.vue +257 -257
  95. package/packages/tpl/index.js +4 -4
  96. package/packages/tpl/src/index.vue +55 -55
  97. package/packages/tree/index.js +4 -4
  98. package/packages/tree/src/TreeNode.vue +29 -29
  99. package/packages/tree/src/index.vue +101 -101
  100. package/packages/tree-select/index.js +4 -4
  101. package/packages/tree-select/src/index.vue +142 -142
  102. package/packages/upload/index.js +4 -4
  103. package/packages/upload/src/index.vue +998 -494
  104. package/packages/utils/README.md +172 -0
  105. package/packages/utils/index.js +66 -62
  106. package/packages/utils/runtime-context/dataPoolAPI.js +501 -0
  107. package/packages/utils/runtime-context/globalDataPool.js +279 -0
  108. package/packages/utils/runtime-context/index.js +76 -0
  109. package/packages/utils/runtime-context/modelSchema.js +174 -0
  110. package/scripts/rollup.config.js +42 -42
  111. package/scripts/rollup.esm.config.js +11 -11
  112. package/scripts/rollup.umd.config.js +17 -14
  113. package/vitest.config.js +17 -0
@@ -1,320 +1,606 @@
1
- <template>
2
- <div class="g-table__wrapper" :style="wrapperStyle" :class="{ 'g-table--no-scroll-y': !needScrollY }">
3
- <a-table
4
- :bordered="bordered"
5
- :class="[isNoData && 'g-table__no-data']"
6
- :pagination="false"
7
- :loading="loading"
8
- size="middle"
9
- :columns="innerColumns"
10
- :row-selection="rowSelection"
11
- :row-class-name="setRowClassName"
12
- :data-source="dataSource"
13
- :scroll="getScroll">
14
- <template slot="action" slot-scope="record">
15
- <Actions v-on="$listeners" :data-source="actions" :record="record"></Actions>
16
- </template>
17
- </a-table>
18
- <div class="g-table__pagination">
19
- <a-pagination
20
- :show-total="all => `共 ${all} 条数据`"
21
- show-size-changer
22
- show-quick-jumper
23
- :pageSize="innerPageSize"
24
- :current="innerCurrentPage"
25
- :pageSizeOptions="pageSizeOptions"
26
- @change="onChangePagination"
27
- @showSizeChange="onShowSizeChange"
28
- :total="total">
29
- </a-pagination>
30
- </div>
31
- </div>
32
- </template>
33
-
34
- <script>
35
- import Actions from './action.vue'
36
- export default {
37
- name: 'ele-table',
38
- components: {
39
- Actions
40
- },
41
- props: {
42
- // ant table wrapper
43
- height: {
44
- type: Number
45
- },
46
- width: {
47
- type: Number
48
- },
49
- x: {
50
- type: Number,
51
- default: 1200
52
- },
53
- y: {
54
- type: Number,
55
- default: 200
56
- },
57
- scroll: {
58
- type: Object
59
- },
60
- rowSelection: {
61
- type: Object
62
- },
63
- actions: {
64
- type: Array,
65
- default: () => []
66
- },
67
- total: {
68
- type: Number,
69
- default: 0
70
- },
71
- loading: {
72
- type: Boolean,
73
- default: false
74
- },
75
- columns: {
76
- type: Array,
77
- default: () => []
78
- },
79
- dataSource: {
80
- type: Array,
81
- default: () => []
82
- },
83
- pageSize: {
84
- type: Number,
85
- default: 10
86
- },
87
- pageSizeOptions: {
88
- type: Array,
89
- default: () => ['10', '20', '30', '40']
90
- },
91
- bordered: {
92
- type: Boolean,
93
- default: true
94
- }
95
- },
96
- data() {
97
- return {
98
- tableHeaderHeight: 0,
99
- paginationHeight: 0,
100
- innerPageSize: 10,
101
- innerCurrentPage: 1,
102
- tableContentHeight: 0,
103
- obs: []
104
- }
105
- },
106
- computed: {
107
- wrapperStyle () {
108
- // 外层容器样式:确保分页始终可见,表格充满剩余空间
109
- if (!this.height) return {}
110
- return { height: `${this.height}px` }
111
- },
112
- needScrollY () {
113
- // 判断是否需要 y 轴滚动:基于数据行数与可用高度预估
114
- if (!this.height) return false
115
-
116
- const availableHeight = this.getScrollHeightByHeight
117
- if (availableHeight <= 0) return false
118
-
119
- // 预估每行高度(包含边框),antd 默认约 54px
120
- const estimatedRowHeight = 54
121
- const estimatedTableHeight = this.dataSource.length * estimatedRowHeight
122
-
123
- return estimatedTableHeight > availableHeight
124
- },
125
- innerColumns () {
126
- return this.columns.filter(col => !Object.keys(col).includes('multiple'))
127
- },
128
- isNoData () {
129
- return !this.dataSource.length
130
- },
131
- getScrollHeightByHeight () {
132
- // 始终返回可用的剩余高度,让表格内容不足时也能占满容器
133
- return this.height - this.tableHeaderHeight - this.paginationHeight
134
- },
135
- isFlexColumn () {
136
- return this.columns.every(col => !col.width)
137
- },
138
- getScroll () {
139
- if (this.scroll) {
140
- return this.scroll
141
- } else {
142
- const baseX = this.isFlexColumn ? 0 : (this.width > this.x ? 0 : this.x)
143
- if (this.height) {
144
- // 计算表体可用高度
145
- const availableHeight = this.tableHeaderHeight && this.paginationHeight
146
- ? this.getScrollHeightByHeight
147
- : this.height - 100 // 预估值,给表头和分页留空间
148
-
149
- if (availableHeight > 50) { // 确保有足够的最小高度
150
- console.log('Table scroll config:', { x: baseX, y: availableHeight, height: this.height, headerHeight: this.tableHeaderHeight, paginationHeight: this.paginationHeight })
151
- return { x: baseX, y: availableHeight }
152
- }
153
- }
154
- return { x: baseX }
155
- }
156
- }
157
- },
158
- watch: {
159
- pageSize: {
160
- handler (pageSize) {
161
- this.innerPageSize = pageSize
162
- },
163
- immediate: true
164
- }
165
- },
166
- methods: {
167
- onShowSizeChange (current, pageSize) {
168
- this.innerCurrentPage = current
169
- this.innerPageSize = pageSize
170
- this.$emit('change-page', current, pageSize)
171
- },
172
- setPaginationHeight () {
173
- this.$nextTick(() => {
174
- const { height } = document.querySelector('.g-table__pagination').getBoundingClientRect()
175
- this.paginationHeight = height
176
- })
177
- },
178
- setTableHeaderHeight () {
179
- this.$nextTick(() => {
180
- const el = document.querySelector('.ant-table-header')
181
- if (!el) return
182
- const { height } = el.getBoundingClientRect()
183
- this.tableHeaderHeight = height
184
- })
185
- },
186
- setTableTbodyHeight () {
187
- // 简化:仅需要获取表头高度,不再依赖表体高度的动态监听
188
- this.$nextTick(() => {
189
- this.setTableHeaderHeight()
190
- })
191
- },
192
- setRowClassName (record, idx) {
193
- return idx % 2 === 0 ? 'g-table__row--even' : 'g-table__row--odd'
194
- },
195
- onChangePagination (page, pageSize) {
196
- this.innerCurrentPage = page
197
- this.innerPageSize = pageSize
198
- this.$emit('change-page', page, pageSize)
199
- }
200
- },
201
- mounted() {
202
- this.$nextTick(() => {
203
- this.setPaginationHeight()
204
- setTimeout(() => {
205
- this.setTableTbodyHeight()
206
- // 再测一次分页高度,确保初次渲染完成后数值准确
207
- this.setPaginationHeight()
208
- }, 200)
209
- })
210
- },
211
- destroyed () {
212
- this.obs.forEach(ob => ob.disconnect())
213
- }
214
- }
215
- </script>
216
-
217
- <style lang="scss" scoped>
218
- .g-table__wrapper {
219
- /* 外层采用列布局,确保分页始终可见,表格充满剩余空间 */
220
- display: flex;
221
- flex-direction: column;
222
- overflow: hidden;
223
-
224
- /* 表格区域占满剩余空间,启用纵向滚动 */
225
- ::v-deep .ant-table-wrapper {
226
- flex: 1 1 auto;
227
- min-height: 0;
228
- display: flex;
229
- flex-direction: column;
230
- }
231
-
232
- ::v-deep .ant-spin-nested-loading {
233
- flex: 1 1 auto;
234
- min-height: 0;
235
- display: flex;
236
- flex-direction: column;
237
- }
238
-
239
- ::v-deep .ant-spin-container {
240
- flex: 1 1 auto;
241
- min-height: 0;
242
- display: flex;
243
- flex-direction: column;
244
- }
245
-
246
- ::v-deep .ant-table {
247
- flex: 1 1 auto;
248
- min-height: 0;
249
- display: flex;
250
- flex-direction: column;
251
- }
252
-
253
- /* 确保表体能够正常纵向滚动 */
254
- ::v-deep .ant-table-content {
255
- flex: 1 1 auto;
256
- min-height: 0;
257
- overflow: hidden;
258
- }
259
-
260
- ::v-deep .ant-table-scroll {
261
- height: 100%;
262
- display: flex;
263
- flex-direction: column;
264
- }
265
-
266
- ::v-deep .ant-table-header {
267
- flex: 0 0 auto;
268
- overflow: hidden;
269
- }
270
- ::v-deep .ant-table-fixed {
271
- .ant-table-tbody {
272
- .g-table__row--odd {
273
- &.ant-table-row-hover {
274
- td {
275
- background-color: var(--idooel-row-hover-color);
276
- }
277
- .ant-table-selection-column {
278
- background-color: var(--idooel-row-hover-color) !important;
279
- }
280
- .ant-table-row-cell-break-word {
281
- background-color: var(--idooel-row-hover-color) !important;
282
- }
283
- }
284
- td {
285
- background-color: var(--idooel-row-odd-color);
286
- }
287
- .ant-table-selection-column {
288
- background-color: var(--idooel-row-odd-color) !important;
289
- }
290
- .ant-table-row-cell-break-word {
291
- background-color: var(--idooel-row-odd-color) !important;
292
- }
293
- }
294
- }
295
- .ant-table-thead {
296
- background: rgba(0, 0, 0, 0.03) !important;
297
- }
298
- }
299
-
300
- ::v-deep .ant-table-body {
301
- flex: 1 1 auto;
302
- overflow: auto !important;
303
- /* 确保滚动条稳定,避免列宽抖动 */
304
- scrollbar-gutter: stable;
305
- }
306
-
307
- /* 分页区域固定在底部,不被挤出视口 */
308
- .g-table__pagination {
309
- flex: 0 0 auto;
310
- display: flex;
311
- flex-direction: row;
312
- justify-content: end;
313
- border-top: unset;
314
- padding-top: 8px;
315
- padding-bottom: 8px;
316
- background: #fff;
317
- z-index: 999;
318
- }
319
- }
1
+ <template>
2
+ <div class="g-table__wrapper" ref="tableWrapper" :style="wrapperStyle">
3
+ <a-table
4
+ :key="tableRenderKey"
5
+ :bordered="bordered"
6
+ :class="[isNoData && 'g-table__no-data']"
7
+ :pagination="false"
8
+ :loading="loading"
9
+ size="middle"
10
+ :columns="smartColumns"
11
+ :row-selection="smartRowSelection"
12
+ :row-class-name="setRowClassName"
13
+ :data-source="dataSource"
14
+ :scroll="smartScroll">
15
+ <template slot="action" slot-scope="record">
16
+ <Actions v-on="$listeners" :data-source="actions" :record="record"></Actions>
17
+ </template>
18
+ </a-table>
19
+ <div class="g-table__pagination">
20
+ <a-pagination
21
+ :show-total="all => `共 ${all} 条数据`"
22
+ v-if="mode === 'default'"
23
+ show-size-changer
24
+ show-quick-jumper
25
+ :pageSize="innerPageSize"
26
+ :current="innerCurrentPage"
27
+ :pageSizeOptions="pageSizeOptions"
28
+ @change="onChangePagination"
29
+ @showSizeChange="onShowSizeChange"
30
+ :total="total">
31
+ </a-pagination>
32
+ <ele-pagination
33
+ v-else
34
+ :pageSize="innerPageSize"
35
+ :current="innerCurrentPage"
36
+ :pageSizeOptions="pageSizeOptions"
37
+ :data="dataSource.length"
38
+ @change="onChangePagination"
39
+ @showSizeChange="onShowSizeChange"
40
+ :total="total"
41
+ ></ele-pagination>
42
+ </div>
43
+ </div>
44
+ </template>
45
+
46
+ <script>
47
+ import Actions from './action.vue'
48
+ export default {
49
+ name: 'ele-table',
50
+ components: {
51
+ Actions
52
+ },
53
+ props: {
54
+ mode: {
55
+ type: String,
56
+ default: 'default',
57
+ validator: (value) => {
58
+ return ['default', 'next-cursor'].includes(value)
59
+ }
60
+ },
61
+ // ant table wrapper
62
+ height: {
63
+ type: Number
64
+ },
65
+ width: {
66
+ type: Number
67
+ },
68
+ x: {
69
+ // ant-design-vue: scroll.x 支持 number | string(如 'max-content')
70
+ type: [Number, String],
71
+ default: 1200
72
+ },
73
+ y: {
74
+ type: Number,
75
+ default: 200
76
+ },
77
+ scroll: {
78
+ type: Object
79
+ },
80
+ rowSelection: {
81
+ type: Object
82
+ },
83
+ actions: {
84
+ type: Array,
85
+ default: () => []
86
+ },
87
+ total: {
88
+ type: Number,
89
+ default: 0
90
+ },
91
+ loading: {
92
+ type: Boolean,
93
+ default: false
94
+ },
95
+ columns: {
96
+ type: Array,
97
+ default: () => []
98
+ },
99
+ dataSource: {
100
+ type: Array,
101
+ default: () => []
102
+ },
103
+ pageSize: {
104
+ type: [Number, String],
105
+ default: 10
106
+ },
107
+ pageSizeOptions: {
108
+ type: Array,
109
+ default: () => ['10', '20', '30', '40']
110
+ },
111
+ bordered: {
112
+ type: Boolean,
113
+ default: true
114
+ }
115
+ },
116
+ data() {
117
+ return {
118
+ tableHeaderHeight: 0,
119
+ paginationHeight: 0,
120
+ innerPageSize: 10,
121
+ innerCurrentPage: 1,
122
+ tableContentHeight: 0,
123
+ obs: [],
124
+ // 容器宽度,用于智能判断是否需要 fixed 列
125
+ containerWidth: 0,
126
+ // 用于强制重新渲染表格(当 fixed 列状态切换时)
127
+ tableRenderKey: 0
128
+ }
129
+ },
130
+ computed: {
131
+ wrapperStyle () {
132
+ // 外层容器样式
133
+ if (!this.height) return {}
134
+ return { height: `${this.height}px` }
135
+ },
136
+ needScrollY () {
137
+ // 判断是否需要 y 轴滚动:基于数据行数与可用高度预估
138
+ if (!this.height) return false
139
+
140
+ const availableHeight = this.getScrollHeightByHeight
141
+ if (availableHeight <= 0) return false
142
+
143
+ // 预估每行高度(包含边框),antd 默认约 54px
144
+ const estimatedRowHeight = 54
145
+ const estimatedTableHeight = this.dataSource.length * estimatedRowHeight
146
+
147
+ return estimatedTableHeight > availableHeight
148
+ },
149
+ innerColumns () {
150
+ return this.columns.filter(col => !Object.keys(col).includes('multiple'))
151
+ },
152
+ isNoData () {
153
+ return !this.dataSource.length
154
+ },
155
+ getScrollHeightByHeight () {
156
+ // 始终返回可用的剩余高度,让表格内容不足时也能占满容器
157
+ return this.height - this.tableHeaderHeight - this.paginationHeight
158
+ },
159
+ isFlexColumn () {
160
+ return this.columns.every(col => !col.width)
161
+ },
162
+ /**
163
+ * 计算所有列的总宽度(包括 rowSelection 的 checkbox 列和操作列)
164
+ */
165
+ totalColumnsWidth () {
166
+ const cols = this.innerColumns || []
167
+ let total = cols.reduce((sum, col) => {
168
+ const w = col && col.width
169
+ return sum + (typeof w === 'number' ? w : 0)
170
+ }, 0)
171
+ // rowSelection 的 checkbox/radio 列,antd 默认约 60px
172
+ if (this.rowSelection) total += 60
173
+ // 操作列(operations)的宽度
174
+ if (this.operations && this.operations.width && typeof this.operations.width === 'number') {
175
+ total += this.operations.width
176
+ }
177
+ return total
178
+ },
179
+ /**
180
+ * 是否需要横向滚动:容器宽度 < 列总宽度
181
+ */
182
+ needHorizontalScroll () {
183
+ // 未获取容器宽度前,先假定需要滚动(保守策略)
184
+ if (!this.containerWidth) return true
185
+ // 加一点容差
186
+ return this.containerWidth < this.totalColumnsWidth - 5
187
+ },
188
+ /**
189
+ * 智能列配置:
190
+ * - 当需要横向滚动时,保留原始 fixed 属性
191
+ * - 当容器足够宽时,移除 fixed 属性,让表格自动铺满
192
+ */
193
+ smartColumns () {
194
+ if (this.needHorizontalScroll) {
195
+ // 需要滚动,保留原始配置
196
+ return this.innerColumns
197
+ }
198
+ // 不需要滚动,移除所有 fixed 属性
199
+ return this.innerColumns.map(col => {
200
+ if (col.fixed) {
201
+ const { fixed, ...rest } = col
202
+ return rest
203
+ }
204
+ return col
205
+ })
206
+ },
207
+ /**
208
+ * 智能 scroll 配置:
209
+ * - 当需要横向滚动时,设置 scroll.x
210
+ * - 当容器足够宽时,不设置 scroll.x,避免产生空白区域
211
+ */
212
+ smartScroll () {
213
+ if (!this.needHorizontalScroll) {
214
+ // 不需要横向滚动,只保留 y 方向(如果需要)
215
+ if (this.height && this.needScrollY) {
216
+ const availableHeight = this.tableHeaderHeight && this.paginationHeight
217
+ ? this.getScrollHeightByHeight
218
+ : this.height - 100
219
+ if (availableHeight > 50) {
220
+ return { y: availableHeight }
221
+ }
222
+ }
223
+ return {}
224
+ }
225
+ // 需要横向滚动,使用原有逻辑
226
+ return this.getScroll
227
+ },
228
+ /**
229
+ * 智能 rowSelection 配置:
230
+ * - 当需要横向滚动时,保留原始 fixed 属性
231
+ * - 当容器足够宽时,移除 fixed 属性
232
+ */
233
+ smartRowSelection () {
234
+ if (!this.rowSelection) return null
235
+ if (this.needHorizontalScroll) {
236
+ return this.rowSelection
237
+ }
238
+ // 不需要滚动,移除 fixed 属性
239
+ if (this.rowSelection.fixed) {
240
+ const { fixed, ...rest } = this.rowSelection
241
+ return rest
242
+ }
243
+ return this.rowSelection
244
+ },
245
+ getScroll () {
246
+ if (this.scroll) {
247
+ return this.scroll
248
+ } else {
249
+ // 固定列需要 scroll.x 才能正确同步行高,始终设置一个有效值
250
+ let baseX = (this.x === '' || this.x === null || this.x === undefined) ? 1200 : this.x
251
+
252
+ // 解决“x 给太大导致操作列前出现大块空白”的问题:
253
+ // 当所有列都给了明确 width 时,scroll.x 取列宽总和最合理;大于总和会产生多余区域。
254
+ if (typeof baseX === 'number') {
255
+ const cols = this.innerColumns || []
256
+ const total = cols.reduce((sum, col) => {
257
+ const w = col && col.width
258
+ return sum + (typeof w === 'number' ? w : 0)
259
+ }, 0)
260
+
261
+ // rowSelection 的 checkbox/radio 列是 antd 自动加的,给一个经验宽度避免误差
262
+ const selectionWidth = this.rowSelection ? 60 : 0
263
+
264
+ // 只有当所有列都明确给了宽度(total > 0)时才 clamp
265
+ if (total > 0) {
266
+ const minX = total + selectionWidth
267
+ if (baseX > minX) baseX = minX
268
+ }
269
+ }
270
+
271
+ if (this.height && this.needScrollY) {
272
+ const availableHeight = this.tableHeaderHeight && this.paginationHeight
273
+ ? this.getScrollHeightByHeight
274
+ : this.height - 100
275
+
276
+ if (availableHeight > 50) {
277
+ return { x: baseX, y: availableHeight }
278
+ }
279
+ }
280
+ return { x: baseX }
281
+ }
282
+ }
283
+ },
284
+ watch: {
285
+ pageSize: {
286
+ handler (pageSize) {
287
+ this.innerPageSize = pageSize
288
+ },
289
+ immediate: true
290
+ },
291
+ /**
292
+ * 监听 needHorizontalScroll 变化,强制重新渲染表格
293
+ * 当从"需要固定列"切换到"不需要固定列"或反之时,antd 的 a-table 需要完全重新渲染
294
+ */
295
+ needHorizontalScroll (newVal, oldVal) {
296
+ if (newVal !== oldVal) {
297
+ // 更新 key 强制 Vue 销毁并重建 a-table 组件
298
+ this.tableRenderKey++
299
+ // 使用重试机制确保固定列完全渲染
300
+ this.$nextTick(() => {
301
+ this.retrySyncFixedColumns(newVal)
302
+ })
303
+ }
304
+ }
305
+ },
306
+ methods: {
307
+ onShowSizeChange (current, pageSize) {
308
+ this.innerCurrentPage = current
309
+ this.innerPageSize = pageSize
310
+ this.$emit('change-page', current, pageSize)
311
+ },
312
+ setPaginationHeight () {
313
+ this.$nextTick(() => {
314
+ const el = this.$el.querySelector('.g-table__pagination')
315
+ if (el) {
316
+ const { height } = el.getBoundingClientRect()
317
+ this.paginationHeight = height
318
+ }
319
+ })
320
+ },
321
+ setTableHeaderHeight () {
322
+ this.$nextTick(() => {
323
+ const el = this.$el.querySelector('.ant-table-header')
324
+ if (!el) return
325
+ const { height } = el.getBoundingClientRect()
326
+ this.tableHeaderHeight = height
327
+ })
328
+ },
329
+ setTableTbodyHeight () {
330
+ this.$nextTick(() => {
331
+ this.setTableHeaderHeight()
332
+ })
333
+ },
334
+ setRowClassName (record, idx) {
335
+ return idx % 2 === 0 ? 'g-table__row--even' : 'g-table__row--odd'
336
+ },
337
+ onChangePagination (page, pageSize) {
338
+ this.innerCurrentPage = page
339
+ this.innerPageSize = pageSize
340
+ this.$emit('change-page', page, pageSize)
341
+ },
342
+ syncFixedColumns () {
343
+ // 强制 ant-design-vue 重新计算固定列的宽度
344
+ this.$nextTick(() => {
345
+ const tableEl = this.$el.querySelector('.ant-table')
346
+ if (tableEl) {
347
+ // 触发窗口 resize 事件,让 ant-design-vue 重新计算
348
+ window.dispatchEvent(new Event('resize'))
349
+ }
350
+ })
351
+ },
352
+ /**
353
+ * 带重试的固定列同步机制
354
+ * antd 的 a-table 在 key 变化后重新渲染固定列需要一定时间,
355
+ * 这里通过重试确保固定列渲染完成后再同步。
356
+ * @param {boolean} needFixed - 是否需要固定列
357
+ * @param {number} retries - 当前重试次数
358
+ */
359
+ retrySyncFixedColumns (needFixed, retries = 0) {
360
+ const MAX_RETRIES = 5
361
+ const DELAY = 100 // 每次延迟 100ms
362
+
363
+ setTimeout(() => {
364
+ this.syncFixedColumns()
365
+ this.syncHeaderTableWidth()
366
+ this.bindScrollSync()
367
+
368
+ // 如果需要固定列,检查是否已渲染
369
+ if (needFixed && retries < MAX_RETRIES) {
370
+ const hasFixedLeft = this.$el.querySelector('.ant-table-fixed-left .ant-table-body tbody tr')
371
+ const hasFixedRight = this.$el.querySelector('.ant-table-fixed-right .ant-table-body tbody tr')
372
+
373
+ // 如果有固定列配置但还没渲染出来,继续重试
374
+ const hasFixedConfig = this.innerColumns.some(c => c.fixed) || (this.rowSelection && this.rowSelection.fixed)
375
+ if (hasFixedConfig && !hasFixedLeft && !hasFixedRight) {
376
+ this.retrySyncFixedColumns(needFixed, retries + 1)
377
+ }
378
+ }
379
+ }, DELAY)
380
+ },
381
+ /**
382
+ * 修复 x: 'max-content' 场景下表头不跟着横向滚动的问题。
383
+ * 原因:ant-design-vue 的 header table 和 body table 各自算 max-content,
384
+ * header 按表头文字算、body 按实际数据算,二者宽度不同时 header 就"滚不动"。
385
+ * 方案:数据渲染后把 header table 的 width 强制设成和 body table 一样。
386
+ */
387
+ syncHeaderTableWidth () {
388
+ this.$nextTick(() => {
389
+ const headerTable = this.$el.querySelector('.ant-table-scroll .ant-table-header table')
390
+ const bodyTable = this.$el.querySelector('.ant-table-scroll .ant-table-body table')
391
+ if (!headerTable || !bodyTable) return
392
+
393
+ const bodyW = bodyTable.getBoundingClientRect().width
394
+ const headerW = headerTable.getBoundingClientRect().width
395
+
396
+ // 始终同步表头宽度到表体宽度,确保窗口变大/变小时都能正确响应
397
+ // 只有当宽度差异超过 2px 时才更新,避免频繁设置样式
398
+ if (Math.abs(bodyW - headerW) > 2) {
399
+ headerTable.style.width = `${bodyW}px`
400
+ headerTable.style.minWidth = `${bodyW}px`
401
+ }
402
+ })
403
+ },
404
+ /**
405
+ * 监听表体横向滚动,同步到表头(防止 antd 自带同步失效)
406
+ */
407
+ bindScrollSync () {
408
+ const body = this.$el.querySelector('.ant-table-scroll .ant-table-body')
409
+ const header = this.$el.querySelector('.ant-table-scroll .ant-table-header')
410
+ if (!body || !header) return
411
+
412
+ if (this._scrollHandler) return // 已绑定
413
+ this._scrollHandler = () => {
414
+ header.scrollLeft = body.scrollLeft
415
+ }
416
+ body.addEventListener('scroll', this._scrollHandler, { passive: true })
417
+ },
418
+ unbindScrollSync () {
419
+ const body = this.$el.querySelector('.ant-table-scroll .ant-table-body')
420
+ if (body && this._scrollHandler) {
421
+ body.removeEventListener('scroll', this._scrollHandler)
422
+ this._scrollHandler = null
423
+ }
424
+ },
425
+ /**
426
+ * 测量容器宽度
427
+ */
428
+ measureContainerWidth () {
429
+ const wrapper = this.$refs.tableWrapper
430
+ if (wrapper) {
431
+ this.containerWidth = wrapper.clientWidth
432
+ }
433
+ },
434
+ /**
435
+ * 使用 ResizeObserver 监听容器宽度变化
436
+ */
437
+ observeContainerWidth () {
438
+ const wrapper = this.$refs.tableWrapper
439
+ if (!wrapper || typeof ResizeObserver === 'undefined') return
440
+
441
+ if (this._containerResizeObserver) return // 已绑定
442
+
443
+ this._containerResizeObserver = new ResizeObserver((entries) => {
444
+ for (const entry of entries) {
445
+ const newWidth = entry.contentRect.width
446
+ // 只有宽度变化超过阈值才触发更新,避免微小变化导致频繁重渲染
447
+ if (Math.abs(newWidth - this.containerWidth) > 10) {
448
+ const oldNeedScroll = this.needHorizontalScroll
449
+ this.containerWidth = newWidth
450
+ // 容器宽度变化会触发 needHorizontalScroll 的重新计算
451
+ // needHorizontalScroll 的 watcher 会处理表格重新渲染和列宽同步
452
+ // 这里不需要直接调用 syncFixedColumns,避免与 watcher 的执行时机冲突
453
+ // 无论 needHorizontalScroll 是否变化,都立即同步表头宽度
454
+ // 确保窗口变大/变小时表头都能及时响应
455
+ this.$nextTick(() => {
456
+ // 使用 requestAnimationFrame 确保在浏览器重绘后同步表头宽度
457
+ // 这样能确保表体宽度已经更新完成
458
+ requestAnimationFrame(() => {
459
+ this.syncHeaderTableWidth()
460
+ })
461
+
462
+ // 如果 needHorizontalScroll 状态没有变化,说明只是列宽需要调整,直接同步
463
+ // 如果状态变化了,watcher 会处理重新渲染
464
+ if (this.needHorizontalScroll === oldNeedScroll) {
465
+ // 防抖:延迟同步固定列,避免频繁调用
466
+ if (this._resizeDebounceTimer) {
467
+ clearTimeout(this._resizeDebounceTimer)
468
+ }
469
+ this._resizeDebounceTimer = setTimeout(() => {
470
+ this.syncFixedColumns()
471
+ this.syncHeaderTableWidth()
472
+ this.bindScrollSync()
473
+ }, 150)
474
+ }
475
+ })
476
+ }
477
+ }
478
+ })
479
+ this._containerResizeObserver.observe(wrapper)
480
+ },
481
+ /**
482
+ * 断开容器宽度监听
483
+ */
484
+ unobserveContainerWidth () {
485
+ if (this._containerResizeObserver) {
486
+ this._containerResizeObserver.disconnect()
487
+ this._containerResizeObserver = null
488
+ }
489
+ // 清理防抖定时器
490
+ if (this._resizeDebounceTimer) {
491
+ clearTimeout(this._resizeDebounceTimer)
492
+ this._resizeDebounceTimer = null
493
+ }
494
+ }
495
+ },
496
+ mounted() {
497
+ this.$nextTick(() => {
498
+ // 先测量容器宽度,用于智能判断是否需要 fixed 列
499
+ this.measureContainerWidth()
500
+ this.observeContainerWidth()
501
+
502
+ this.setPaginationHeight()
503
+ setTimeout(() => {
504
+ this.setTableTbodyHeight()
505
+ this.setPaginationHeight()
506
+ // 强制同步固定列和主表的列宽
507
+ this.syncFixedColumns()
508
+ // 同步表头 table 宽度(修复 max-content 场景)
509
+ this.syncHeaderTableWidth()
510
+ // 绑定横向滚动同步
511
+ this.bindScrollSync()
512
+ }, 200)
513
+ })
514
+
515
+ // 监听数据变化,重新同步列宽
516
+ this.$watch('dataSource', () => {
517
+ this.$nextTick(() => {
518
+ setTimeout(() => {
519
+ this.syncFixedColumns()
520
+ this.syncHeaderTableWidth()
521
+ this.bindScrollSync()
522
+ }, 100)
523
+ })
524
+ }, { deep: true })
525
+ },
526
+ destroyed () {
527
+ this.obs.forEach(ob => ob.disconnect())
528
+ this.unbindScrollSync()
529
+ this.unobserveContainerWidth()
530
+ }
531
+ }
532
+ </script>
533
+
534
+ <style lang="scss" scoped>
535
+ .g-table__wrapper {
536
+ /**
537
+ * 修复"宽屏下表格两侧出现空白"问题:
538
+ * 当视口宽度大于表格内容宽度时,主表(ant-table-scroll)不会自动拉伸,
539
+ * 而固定列(fixed-left/fixed-right)是 position:absolute 定位在容器边缘,中间就出现空白。
540
+ * 解决方案:让主表的 table 元素 min-width:100%,使其始终填满滚动容器。
541
+ */
542
+ ::v-deep .ant-table-scroll .ant-table-header table,
543
+ ::v-deep .ant-table-scroll .ant-table-body table {
544
+ min-width: 100%;
545
+ }
546
+
547
+ /**
548
+ * 修复"固定列 + scroll.x"场景下,主表(ant-table-scroll)里会渲染一份 fixed 列的占位 header/cell。
549
+ * 这份占位本来只用于计算宽度,但在某些布局下会被看见,表现为"操作列前多了一大块空白/空列"。
550
+ * 这里用 visibility:hidden 隐藏占位(不影响占位宽度与 fixed 计算),避免视觉空白。
551
+ */
552
+ ::v-deep .ant-table-scroll .ant-table-header thead > tr > th.ant-table-fixed-columns-in-body.ant-table-row-cell-last,
553
+ ::v-deep .ant-table-scroll .ant-table-body tbody > tr > td.ant-table-fixed-columns-in-body.ant-table-row-cell-last {
554
+ visibility: hidden;
555
+ }
556
+
557
+ /* 强制统一行高,确保主表和固定列对齐 */
558
+ ::v-deep .ant-table-tbody > tr > td {
559
+ height: 54px;
560
+ padding: 8px 16px;
561
+ vertical-align: middle;
562
+ box-sizing: border-box;
563
+ line-height: 38px;
564
+ }
565
+
566
+ /* 表头也统一高度和样式 */
567
+ ::v-deep .ant-table-thead > tr > th {
568
+ height: 54px;
569
+ padding: 8px 16px;
570
+ vertical-align: middle;
571
+ box-sizing: border-box;
572
+ line-height: 38px;
573
+ }
574
+
575
+ /* 分页区域固定在底部 */
576
+ .g-table__pagination {
577
+ display: flex;
578
+ flex-direction: row;
579
+ justify-content: end;
580
+ border-top: unset;
581
+ padding-top: 8px;
582
+ padding-bottom: 8px;
583
+ background: #fff;
584
+ }
585
+
586
+ /* 空数据状态顶部显示 */
587
+ .g-table__no-data {
588
+ position: relative;
589
+ ::v-deep .ant-table-placeholder {
590
+ position: absolute;
591
+ top: 50%;
592
+ left: 50%;
593
+ transform: translate(-50%, -40%);
594
+ width: 100%;
595
+ height: 100%;
596
+ text-align: center;
597
+ color: #999;
598
+ font-size: 14px;
599
+ font-weight: normal;
600
+ line-height: 20px;
601
+ overflow: hidden;
602
+ border: unset;
603
+ }
604
+ }
605
+ }
320
606
  </style>