完成90%页面分页组件的抽取
This commit is contained in:
@@ -37,7 +37,7 @@
|
||||
</view>
|
||||
|
||||
<view class="table-body">
|
||||
<view class="table-row" v-for="item in categoryList" :key="item.id">
|
||||
<view class="table-row" v-for="item in pagedList" :key="item.id">
|
||||
<view class="td col-id"><text class="td-txt">{{ item.id }}</text></view>
|
||||
<view class="td col-name"><text class="td-txt">{{ item.name }}</text></view>
|
||||
<view class="td col-img">
|
||||
@@ -57,6 +57,22 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<CommonPagination
|
||||
v-if="total > 0"
|
||||
: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>
|
||||
|
||||
@@ -135,15 +151,68 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ref } from 'vue'
|
||||
import { ref, computed } from 'vue'
|
||||
import StatusSwitch from '@/components/StatusSwitch.uvue'
|
||||
import CommonPagination from '@/components/CommonPagination/CommonPagination.uvue'
|
||||
|
||||
const filterKeyword = ref('')
|
||||
// ========== MOCK DATA START ==========
|
||||
// TODO: 接真实接口时替换此处 categoryList 为 fetchCategoryList() 调用
|
||||
const categoryList = ref([
|
||||
{ id: '181', name: '购物心得', status: true },
|
||||
{ id: '180', name: '消费文化', status: true },
|
||||
{ id: '179', name: '品牌资讯', status: true }
|
||||
{ id: '200', name: '购物心得', status: true },
|
||||
{ id: '199', name: '消费文化', status: true },
|
||||
{ id: '198', name: '品牌资讯', status: true },
|
||||
{ id: '197', name: '新品上市', status: true },
|
||||
{ id: '196', name: '生活方式', status: false },
|
||||
{ id: '195', name: '健康养生', status: true },
|
||||
{ id: '194', name: '科技数码', status: true },
|
||||
{ id: '193', name: '家居家装', status: false },
|
||||
{ id: '192', name: '美食美味', status: true },
|
||||
{ id: '191', name: '旅行户外', status: true },
|
||||
{ id: '190', name: '时尚穿搭', status: true },
|
||||
{ id: '189', name: '亲子活动', status: false },
|
||||
{ id: '188', name: '身心健康', status: true },
|
||||
{ id: '187', name: '财经商业', status: true },
|
||||
{ id: '186', name: '文化艺术', status: true },
|
||||
{ id: '185', name: '社交情感', status: false },
|
||||
{ id: '184', name: '工具技能', status: true },
|
||||
{ id: '183', name: '玩具游戏', status: true },
|
||||
{ id: '182', name: '守护联盟', status: true },
|
||||
{ id: '181', name: '全部分类', status: 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(() => categoryList.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 categoryList.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 ==========
|
||||
|
||||
const showDrawer = ref(false)
|
||||
const isClosing = ref(false)
|
||||
|
||||
Reference in New Issue
Block a user