fix(分页): 修复分页组件中禁用字段仍显示的问题
重构分页逻辑,将原始分页数据与过滤后的分页数据分离 添加对分页变化的监听,自动调整当前页索引和分页状态
Showing
1 changed file
with
34 additions
and
11 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2022-07-18 10:22:22 | 2 | * @Date: 2022-07-18 10:22:22 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-11-18 17:09:46 | 4 | + * @LastEditTime: 2025-11-18 18:02:29 |
| 5 | * @FilePath: /data-table/src/views/index.vue | 5 | * @FilePath: /data-table/src/views/index.vue |
| 6 | * @Description: 首页 | 6 | * @Description: 首页 |
| 7 | --> | 7 | --> |
| ... | @@ -28,7 +28,7 @@ | ... | @@ -28,7 +28,7 @@ |
| 28 | <component v-for="(item, index) in visible_form_data" :id="item.key" :ref="(el) => setRefMap(el, item)" :key="index" | 28 | <component v-for="(item, index) in visible_form_data" :id="item.key" :ref="(el) => setRefMap(el, item)" :key="index" |
| 29 | :is="item.component" :item="item" @active="onActive" @remove="onRemove" @blur="onBlur" /> | 29 | :is="item.component" :item="item" @active="onActive" @remove="onRemove" @blur="onBlur" /> |
| 30 | </van-cell-group> | 30 | </van-cell-group> |
| 31 | - <pagination-field v-if="enable_pagination" :current="current_page_index" :total="pages.length" :is-last="current_page_index === pages.length - 1" @prev="handlePrev" @next="handleNext" @submit="handleSubmit" /> | 31 | + <pagination-field v-if="enable_pagination" :current="current_page_index" :total="filtered_pages.length" :is-last="current_page_index === filtered_pages.length - 1" @prev="handlePrev" @next="handleNext" @submit="handleSubmit" /> |
| 32 | <!-- 非流程版表单 --> | 32 | <!-- 非流程版表单 --> |
| 33 | <div v-if="formData.length && PCommit.visible && !formSetting.is_flow && !enable_pagination" style="margin: 16px"> | 33 | <div v-if="formData.length && PCommit.visible && !formSetting.is_flow && !enable_pagination" style="margin: 16px"> |
| 34 | <van-button round block type="primary" native-type="submit" :disabled="submitStatus"> | 34 | <van-button round block type="primary" native-type="submit" :disabled="submitStatus"> |
| ... | @@ -240,13 +240,23 @@ const PCommit = ref({}); | ... | @@ -240,13 +240,23 @@ const PCommit = ref({}); |
| 240 | * 表单结构数据 | 240 | * 表单结构数据 |
| 241 | */ | 241 | */ |
| 242 | const formData = ref([]); | 242 | const formData = ref([]); |
| 243 | -const pages = ref([]); | 243 | +const pages_raw = ref([]); |
| 244 | // 分页配置 | 244 | // 分页配置 |
| 245 | const enable_pagination = ref(false); | 245 | const enable_pagination = ref(false); |
| 246 | const current_page_index = ref(0); | 246 | const current_page_index = ref(0); |
| 247 | +const filtered_pages = computed(() => { | ||
| 248 | + if (!pages_raw.value.length) { | ||
| 249 | + const keys = formData.value.filter(i => !i.component_props?.disabled).map(i => i.key); | ||
| 250 | + return [keys]; | ||
| 251 | + } | ||
| 252 | + const result = pages_raw.value.map(keys => keys.filter(k => { | ||
| 253 | + const f = formData.value.find(i => i.key === k); | ||
| 254 | + return f && !f.component_props?.disabled; | ||
| 255 | + })).filter(keys => keys.length > 0); | ||
| 256 | + return result.length ? result : [[]]; | ||
| 257 | +}); | ||
| 247 | const visible_keys = computed(() => { | 258 | const visible_keys = computed(() => { |
| 248 | - if (!pages.value.length) return formData.value.map(i => i.key); | 259 | + return filtered_pages.value[current_page_index.value] || []; |
| 249 | - return pages.value[current_page_index.value] || []; | ||
| 250 | }); | 260 | }); |
| 251 | const visible_form_data = computed(() => { | 261 | const visible_form_data = computed(() => { |
| 252 | const set = new Set(visible_keys.value); | 262 | const set = new Set(visible_keys.value); |
| ... | @@ -491,10 +501,10 @@ onMounted(async () => { | ... | @@ -491,10 +501,10 @@ onMounted(async () => { |
| 491 | 501 | ||
| 492 | formData.value = formatData(page_form); | 502 | formData.value = formatData(page_form); |
| 493 | /** | 503 | /** |
| 494 | - * * TAG: 构建分页 | 504 | + * * TAG: 构建分页组件 |
| 495 | */ | 505 | */ |
| 496 | buildPages(); | 506 | buildPages(); |
| 497 | - enable_pagination.value = pages.value.length > 1; | 507 | + enable_pagination.value = filtered_pages.value.length > 1; |
| 498 | /**** END ****/ | 508 | /**** END ****/ |
| 499 | 509 | ||
| 500 | // TAG:获取原来表单数据 | 510 | // TAG:获取原来表单数据 |
| ... | @@ -1377,6 +1387,19 @@ watch( | ... | @@ -1377,6 +1387,19 @@ watch( |
| 1377 | } | 1387 | } |
| 1378 | ); | 1388 | ); |
| 1379 | 1389 | ||
| 1390 | +// 监听分页变化,移除空页并纠正当前页索引与分页状态 | ||
| 1391 | +watch( | ||
| 1392 | + () => filtered_pages.value, | ||
| 1393 | + (newPages) => { | ||
| 1394 | + const last = newPages.length - 1; | ||
| 1395 | + if (current_page_index.value > last) { | ||
| 1396 | + current_page_index.value = last >= 0 ? last : 0; | ||
| 1397 | + } | ||
| 1398 | + enable_pagination.value = newPages.length > 1; | ||
| 1399 | + }, | ||
| 1400 | + { flush: 'post' } | ||
| 1401 | +); | ||
| 1402 | + | ||
| 1380 | // 为每个表单字段创建单独的监听器 | 1403 | // 为每个表单字段创建单独的监听器 |
| 1381 | // 可以监听到简单组件的字段的值变化,自定义的组件无法监听到 | 1404 | // 可以监听到简单组件的字段的值变化,自定义的组件无法监听到 |
| 1382 | const setupFieldWatchers = () => { | 1405 | const setupFieldWatchers = () => { |
| ... | @@ -1429,7 +1452,7 @@ const setVolunteerData = async (volunteer_phone) => { | ... | @@ -1429,7 +1452,7 @@ const setVolunteerData = async (volunteer_phone) => { |
| 1429 | } | 1452 | } |
| 1430 | } | 1453 | } |
| 1431 | 1454 | ||
| 1432 | -// 构建分页 | 1455 | +// 构建分页组件 |
| 1433 | const buildPages = () => { | 1456 | const buildPages = () => { |
| 1434 | const result = []; | 1457 | const result = []; |
| 1435 | let cur = []; | 1458 | let cur = []; |
| ... | @@ -1451,9 +1474,9 @@ const buildPages = () => { | ... | @@ -1451,9 +1474,9 @@ const buildPages = () => { |
| 1451 | for (let i = 0; i < keys.length; i += size) { | 1474 | for (let i = 0; i < keys.length; i += size) { |
| 1452 | chunked.push(keys.slice(i, i + size)); | 1475 | chunked.push(keys.slice(i, i + size)); |
| 1453 | } | 1476 | } |
| 1454 | - pages.value = chunked.length ? chunked : [keys]; | 1477 | + pages_raw.value = chunked.length ? chunked : [keys]; |
| 1455 | } else { | 1478 | } else { |
| 1456 | - pages.value = result; | 1479 | + pages_raw.value = result; |
| 1457 | } | 1480 | } |
| 1458 | }; | 1481 | }; |
| 1459 | 1482 | ||
| ... | @@ -1492,7 +1515,7 @@ const validateCurrentPage = async () => { | ... | @@ -1492,7 +1515,7 @@ const validateCurrentPage = async () => { |
| 1492 | const handleNext = async () => { | 1515 | const handleNext = async () => { |
| 1493 | const ok = await validateCurrentPage(); | 1516 | const ok = await validateCurrentPage(); |
| 1494 | if (!ok) return; | 1517 | if (!ok) return; |
| 1495 | - if (current_page_index.value < pages.value.length - 1) { | 1518 | + if (current_page_index.value < filtered_pages.value.length - 1) { |
| 1496 | current_page_index.value += 1; | 1519 | current_page_index.value += 1; |
| 1497 | image_uploader.value = []; | 1520 | image_uploader.value = []; |
| 1498 | file_uploader.value = []; | 1521 | file_uploader.value = []; | ... | ... |
-
Please register or login to post a comment