完成全部页面分页组件的抽取

This commit is contained in:
2026-03-17 10:50:42 +08:00
parent 7211fcdfea
commit e266482f88
17 changed files with 619 additions and 52 deletions

View File

@@ -20,7 +20,7 @@
</view>
<view class="table-body">
<view v-for="item in dataList" :key="item.id" class="table-row">
<view v-for="item in pagedList" :key="item.id" class="table-row">
<view class="col col-id"><text>{{ item.id }}</text></view>
<view class="col col-name">
<text class="expand-icon">˃</text>
@@ -38,13 +38,32 @@
</view>
</view>
</view>
<CommonPagination
v-if="true"
:total="total"
:loading="false"
:currentPage="currentPage"
:pageSize="pageSize"
:pageSizeOptionLabels="pageSizeOptionLabels"
:pageSizeIndex="pageSizeIndex"
:visiblePages="visiblePages"
:totalPage="totalPage"
:jumpPageInput="jumpPageInput"
@page-size-change="handlePageSizeChange"
@page-change="handlePageChange"
@update:jumpPageInput="(val: string) => { jumpPageInput.value = val }"
@jump-page="handleJumpPage"
/>
</view>
</view>
</template>
<script setup lang="uts">
import { ref } from 'vue'
import { ref, computed } from 'vue'
import CommonPagination from '@/components/CommonPagination/CommonPagination.uvue'
// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 dataList 为 fetchCityList() 调用
const dataList = ref([
{ id: 1, name: '北京市', parentName: '中国' },
{ id: 2, name: '天津市', parentName: '中国' },
@@ -59,8 +78,61 @@ const dataList = ref([
{ id: 11, name: '浙江省', parentName: '中国' },
{ id: 12, name: '安徽省', parentName: '中国' },
{ id: 13, name: '福建省', parentName: '中国' },
{ id: 14, name: '江西省', parentName: '中国' }
{ id: 14, name: '江西省', parentName: '中国' },
{ id: 15, name: '山东省', parentName: '中国' },
{ id: 16, name: '河南省', parentName: '中国' },
{ id: 17, name: '湖北省', parentName: '中国' },
{ id: 18, name: '湖南省', parentName: '中国' },
{ id: 19, name: '广东省', parentName: '中国' },
{ id: 20, name: '广西壮族自治区', parentName: '中国' },
{ id: 21, name: '海南省', parentName: '中国' },
{ id: 22, name: '重庆市', parentName: '中国' },
{ id: 23, name: '四川省', parentName: '中国' },
{ id: 24, name: '贵州省', parentName: '中国' },
{ id: 25, name: '云南省', parentName: '中国' },
{ id: 26, name: '西藏自治区', parentName: '中国' },
{ id: 27, name: '陕西省', parentName: '中国' },
{ id: 28, name: '甘肃省', parentName: '中国' },
{ id: 29, name: '青海省', parentName: '中国' },
{ id: 30, name: '宁夏回族自治区', parentName: '中国' },
{ id: 31, name: '新疆维吾尔自治区', parentName: '中国' },
{ id: 32, name: '香港特别行政区', parentName: '中国' },
{ id: 33, name: '澳门特别行政区', parentName: '中国' },
{ id: 34, name: '台湾省', parentName: '中国' }
])
// ========== MOCK DATA END ==========
// ========== PAGINATION STATE ==========
const currentPage = ref(1)
const pageSize = ref(15)
const jumpPageInput = ref('')
const pageSizeOptions = [10, 15, 20, 30, 50]
const pageSizeOptionLabels = computed(() => pageSizeOptions.map((n: number) => `${n}条/页`))
const pageSizeIndex = computed(() => { const idx = pageSizeOptions.indexOf(pageSize.value); return idx >= 0 ? idx : 0 })
const total = computed(() => dataList.value.length)
const totalPage = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
const pagedList = computed(() => {
const start = (currentPage.value - 1) * pageSize.value
return dataList.value.slice(start, start + pageSize.value)
})
const visiblePages = computed((): number[] => {
const t = totalPage.value; const cur = currentPage.value
if (t <= 7) return Array.from({ length: t }, (_: any, i: number) => i + 1)
if (cur <= 4) return [1, 2, 3, 4, 5, -1, t]
if (cur >= t - 3) return [1, -1, t - 4, t - 3, t - 2, t - 1, t]
return [1, -1, cur - 1, cur, cur + 1, -1, t]
})
const handlePageChange = (p: number) => { currentPage.value = p }
const handlePageSizeChange = (e: any) => {
const idx = Number(e.detail.value)
pageSize.value = pageSizeOptions[idx] ?? pageSizeOptions[0]
currentPage.value = 1
}
const handleJumpPage = () => {
const p = parseInt(jumpPageInput.value)
if (!isNaN(p) && p >= 1 && p <= totalPage.value) currentPage.value = p
}
// ========== END PAGINATION STATE ==========
function addProvince() {
uni.showToast({ title: '添加省份', icon: 'none' })
@@ -173,7 +245,7 @@ function deleteItem(item: any) {
.col-id { width: 100px; }
.col-name { flex: 2; }
.col-parent { flex: 1; }
.col-action { width: 200px; justify-content: flex-end; }
.col-action { width: 200px; justify-content: flex-end; display: flex; flex-direction: row; }
.expand-icon {
margin-right: 8px;

View File

@@ -37,7 +37,7 @@
</view>
</view>
<CommonPagination
v-if="total > 0"
v-if="true"
:total="total"
:loading="false"
:currentPage="currentPage"
@@ -145,6 +145,7 @@ function onDel(item: LogisticsItem) {
.table-wrap { width: 100%; border: 1px solid #f0f0f0; }
.table-header { display: flex; flex-direction: row; background-color: #f8f8f9; }
.th { padding: 12px 10px; font-size: 13px; font-weight: bold; color: #515a6e; border-bottom: 1px solid #f0f0f0; text-align: center; }
.table-body { overflow-y: auto; max-height: calc(100vh - 380px); }
.tr { display: flex; flex-direction: row; align-items: center; border-bottom: 1px solid #f0f0f0; min-height: 50px; }
.td { padding: 10px; font-size: 13px; color: #515a6e; text-align: center; }
.action-btn { font-size: 13px; color: #1890ff; margin-right: 8px; cursor: pointer; }

View File

@@ -37,7 +37,7 @@
</view>
<view class="table-body">
<view v-for="item in dataList" :key="item.id" class="table-row">
<view v-for="item in pagedList" :key="item.id" class="table-row">
<view class="col col-id"><text>{{ item.id }}</text></view>
<view class="col col-name"><text>{{ item.name }}</text></view>
<view class="col col-code"><text>{{ item.code }}</text></view>
@@ -52,18 +52,33 @@
</view>
</view>
<!-- 分页 (模拟) -->
<view class="pagination">
<!-- 这里可以放分页组件 -->
</view>
<CommonPagination
v-if="true"
:total="total"
:loading="false"
:currentPage="currentPage"
:pageSize="pageSize"
:pageSizeOptionLabels="pageSizeOptionLabels"
:pageSizeIndex="pageSizeIndex"
:visiblePages="visiblePages"
:totalPage="totalPage"
:jumpPageInput="jumpPageInput"
@page-size-change="handlePageSizeChange"
@page-change="handlePageChange"
@update:jumpPageInput="(val: string) => { jumpPageInput.value = val }"
@jump-page="handleJumpPage"
/>
</view>
</view>
</view>
</template>
<script setup lang="uts">
import { ref } from 'vue'
import { ref, computed } from 'vue'
import CommonPagination from '@/components/CommonPagination/CommonPagination.uvue'
// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 dataList 为 fetchLogisticsList() 调用
const dataList = ref([
{ id: 2, name: '顺丰速运', code: 'shunfeng', sort: 0, show: true },
{ id: 3, name: '圆通速递', code: 'yuantong', sort: 0, show: true },
@@ -77,8 +92,48 @@ const dataList = ref([
{ id: 11, name: 'EMS', code: 'ems', sort: 0, show: true },
{ id: 12, name: '德邦物流', code: 'debangwuliu', sort: 0, show: true },
{ id: 13, name: '德邦快递', code: 'debangkuaidi', sort: 0, show: true },
{ id: 14, name: '众邮快递', code: 'zhongyouex', sort: 0, show: true }
{ id: 14, name: '众邮快递', code: 'zhongyouex', sort: 0, show: true },
{ id: 15, name: '安能快运', code: 'annengkuaiyun', sort: 0, show: true },
{ id: 16, name: '壹米滴答', code: 'yimidida', sort: 0, show: false },
{ id: 17, name: '宅急送', code: 'zhaijisong', sort: 0, show: true },
{ id: 18, name: '苏宁物流', code: 'suning', sort: 0, show: true },
{ id: 19, name: '中通快运', code: 'zhongtongkuaiyun', sort: 0, show: true },
{ id: 20, name: '芝麻开门', code: 'zhimakaimen', sort: 0, show: false },
{ id: 21, name: '优速快递', code: 'youshuwuliu', sort: 0, show: true }
])
// ========== MOCK DATA END ==========
// ========== PAGINATION STATE ==========
const currentPage = ref(1)
const pageSize = ref(15)
const jumpPageInput = ref('')
const pageSizeOptions = [10, 15, 20, 30, 50]
const pageSizeOptionLabels = computed(() => pageSizeOptions.map((n: number) => `${n}条/页`))
const pageSizeIndex = computed(() => { const idx = pageSizeOptions.indexOf(pageSize.value); return idx >= 0 ? idx : 0 })
const total = computed(() => dataList.value.length)
const totalPage = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
const pagedList = computed(() => {
const start = (currentPage.value - 1) * pageSize.value
return dataList.value.slice(start, start + pageSize.value)
})
const visiblePages = computed((): number[] => {
const t = totalPage.value; const cur = currentPage.value
if (t <= 7) return Array.from({ length: t }, (_: any, i: number) => i + 1)
if (cur <= 4) return [1, 2, 3, 4, 5, -1, t]
if (cur >= t - 3) return [1, -1, t - 4, t - 3, t - 2, t - 1, t]
return [1, -1, cur - 1, cur, cur + 1, -1, t]
})
const handlePageChange = (p: number) => { currentPage.value = p }
const handlePageSizeChange = (e: any) => {
const idx = Number(e.detail.value)
pageSize.value = pageSizeOptions[idx] ?? pageSizeOptions[0]
currentPage.value = 1
}
const handleJumpPage = () => {
const p = parseInt(jumpPageInput.value)
if (!isNaN(p) && p >= 1 && p <= totalPage.value) currentPage.value = p
}
// ========== END PAGINATION STATE ==========
function onSearch() {
uni.showToast({ title: '查询中...', icon: 'none' })