@idooel/components 0.0.2 → 0.0.3-beta.2

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