|
1 | | -import { defineComponent, computed, ref, watch, toRefs, getCurrentInstance } from 'vue'; |
| 1 | +import { defineComponent, computed, ref, watch, toRefs, getCurrentInstance, nextTick } from 'vue'; |
2 | 2 | import { isNaN, isObject } from 'lodash-es'; |
3 | 3 | import { |
4 | 4 | PageFirstIcon as TdPageFirstIcon, |
@@ -183,37 +183,46 @@ export default defineComponent({ |
183 | 183 | }; |
184 | 184 |
|
185 | 185 | const onSelectorChange: (e: string) => void = (e) => { |
186 | | - if (props.disabled) { |
187 | | - return; |
188 | | - } |
189 | | - const pageSize: number = parseInt(e, 10); |
190 | | - let pageCount = 1; |
191 | | - if (pageSize > 0) { |
192 | | - pageCount = Math.max(Math.ceil(props.total / pageSize), 1); |
193 | | - } |
| 186 | + if (props.disabled) return; |
194 | 187 |
|
195 | | - let isIndexChange = false; |
| 188 | + const pageSize: number = parseInt(e, 10); |
| 189 | + const newPageCount = pageSize > 0 ? Math.max(Math.ceil(props.total / pageSize), 1) : 1; |
196 | 190 |
|
197 | | - if (innerCurrent.value > pageCount) { |
198 | | - isIndexChange = true; |
199 | | - } |
| 191 | + // 当前页在变更 pageSize 之后是否会超出新的页码范围 |
| 192 | + const prevCurrent = innerCurrent.value; |
200 | 193 |
|
201 | 194 | /** |
202 | 195 | * 分页大小变化事件 |
203 | 196 | * @param {Number} pageSize 分页大小 |
204 | 197 | * @param {Number} index 当前页 |
205 | 198 | */ |
206 | 199 | const pageInfo = { |
207 | | - current: isIndexChange ? pageCount : innerCurrent.value, |
208 | | - previous: innerCurrent.value, |
| 200 | + current: prevCurrent, |
| 201 | + previous: prevCurrent, |
209 | 202 | pageSize, |
210 | 203 | }; |
| 204 | + |
211 | 205 | setInnerPageSize(pageSize, pageInfo); |
212 | | - if (isIndexChange) { |
213 | | - toPage(pageCount, pageInfo); |
214 | | - } else { |
215 | | - props.onChange?.(pageInfo); |
216 | | - } |
| 206 | + |
| 207 | + nextTick(() => { |
| 208 | + // 使用外部受控的 props.current,如果没有则使用 prevCurrent |
| 209 | + const finalCurrent = props.current ?? prevCurrent; |
| 210 | + const willExceed = finalCurrent > newPageCount; |
| 211 | + const finalPageInfo = { |
| 212 | + ...pageInfo, |
| 213 | + current: finalCurrent, |
| 214 | + }; |
| 215 | + |
| 216 | + if (willExceed) { |
| 217 | + // 当前页超出新页码范围时,跳转到最后一页 |
| 218 | + toPage(newPageCount, finalPageInfo); |
| 219 | + } else if (finalCurrent !== prevCurrent) { |
| 220 | + toPage(finalCurrent, finalPageInfo); |
| 221 | + } else { |
| 222 | + // 正常触发 onChange |
| 223 | + props.onChange?.(finalPageInfo); |
| 224 | + } |
| 225 | + }); |
217 | 226 | }; |
218 | 227 |
|
219 | 228 | const onJumperChange = (val: number) => { |
|
0 commit comments