155 lines
4.1 KiB
Vue
155 lines
4.1 KiB
Vue
<template>
|
||
<view class="pager">
|
||
<view class="pager__info">
|
||
共 {{ total }} {{ unit }}<text class="pager__sep">·</text>{{ loading ? '加载中…' : `第 ${page}/${pageCount} 页` }}
|
||
</view>
|
||
<view class="pager__actions">
|
||
<view
|
||
v-for="btn in buttons"
|
||
:key="btn.label"
|
||
class="pager__btn"
|
||
:class="{ 'pager__btn--disabled': btn.disabled }"
|
||
@click="onTap(btn)"
|
||
>
|
||
{{ btn.label }}
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
/**
|
||
* 底部分页器(固定定位,一行放下)—— 列表页共用
|
||
*
|
||
* 替代 Web 端表格下方的 `el-pagination`,按移动端取舍:
|
||
* - **不做每页条数选择器 / 跳页**(每页条数由调用方的接口常量固定)
|
||
* - 只保留 总条数 + 当前页/总页数 + 「首页 / 上一页 / 下一页 / 尾页」四个**小按钮**
|
||
* - 到边界或加载中自动置灰(不让用户点出无效请求)
|
||
*
|
||
* ⚠️ 组件是 `position: fixed; bottom: 0`,**父页面要给底部留白**(本页用到时给
|
||
* `.page { padding-bottom: 140rpx }`),否则最后一张卡会被盖住。
|
||
* `env(safe-area-inset-bottom)` 已在这里处理,父页面不用管。
|
||
*
|
||
* 用法:
|
||
* ```vue
|
||
* <page-pager
|
||
* v-if="list.length"
|
||
* :total="total"
|
||
* :page="pageNo"
|
||
* :page-size="EXTERNAL_STOCK_PAGE_SIZE"
|
||
* :loading="loading"
|
||
* unit="件商品"
|
||
* @change="goPage"
|
||
* />
|
||
* ```
|
||
*/
|
||
|
||
import { computed } from 'vue'
|
||
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
/** 后端总数 */
|
||
total: number
|
||
/** 当前页码(1-based) */
|
||
page: number
|
||
/** 每页条数(固定值,由调用方的接口常量决定) */
|
||
pageSize: number
|
||
/** 是否正在翻页(置灰 + 显示「加载中…」) */
|
||
loading?: boolean
|
||
/** 数量单位:条 / 件商品 / 张收货单 */
|
||
unit?: string
|
||
}>(),
|
||
{
|
||
loading: false,
|
||
unit: '条'
|
||
}
|
||
)
|
||
|
||
const emit = defineEmits<{ (e: 'change', page: number): void }>()
|
||
|
||
/** 总页数(总数 0 时也算 1 页,避免出现「第 1/0 页」) */
|
||
const pageCount = computed(() =>
|
||
Math.max(1, Math.ceil(props.total / Math.max(1, props.pageSize)))
|
||
)
|
||
|
||
const canPrev = computed(() => props.page > 1)
|
||
const canNext = computed(() => props.page < pageCount.value)
|
||
|
||
/** 四个按钮(首页 / 上一页 / 下一页 / 尾页) */
|
||
const buttons = computed(() => {
|
||
const busy = props.loading
|
||
return [
|
||
{ label: '首页', target: 1, disabled: busy || !canPrev.value },
|
||
{ label: '上一页', target: props.page - 1, disabled: busy || !canPrev.value },
|
||
{ label: '下一页', target: props.page + 1, disabled: busy || !canNext.value },
|
||
{ label: '尾页', target: pageCount.value, disabled: busy || !canNext.value }
|
||
]
|
||
})
|
||
|
||
const onTap = (btn: { label: string; target: number; disabled: boolean }): void => {
|
||
if (btn.disabled) return
|
||
emit('change', btn.target)
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
/* 固定底部,**一行**放下(按钮刻意做小,不占两行高度) */
|
||
.pager {
|
||
position: fixed;
|
||
left: 0;
|
||
right: 0;
|
||
bottom: 0;
|
||
z-index: 20;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
padding: 12rpx 20rpx;
|
||
/* iPhone 底部安全区:不支持 env() 时保留上一行的 12rpx */
|
||
padding-bottom: calc(12rpx + env(safe-area-inset-bottom));
|
||
background-color: #ffffff;
|
||
border-top: 1rpx solid #ebeef5;
|
||
|
||
/* 总数 / 页码:可收缩,极端情况下省略号,保证四个按钮永远完整可见 */
|
||
&__info {
|
||
flex: 1;
|
||
min-width: 0;
|
||
margin-right: 12rpx;
|
||
font-size: 22rpx;
|
||
color: #909399;
|
||
overflow: hidden;
|
||
white-space: nowrap;
|
||
text-overflow: ellipsis;
|
||
}
|
||
|
||
&__sep {
|
||
margin: 0 6rpx;
|
||
}
|
||
|
||
&__actions {
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
&__btn {
|
||
margin-left: 8rpx;
|
||
padding: 8rpx 16rpx;
|
||
font-size: 22rpx;
|
||
line-height: 1.4;
|
||
color: #2979ff;
|
||
text-align: center;
|
||
background-color: #f5f6fa;
|
||
border-radius: 8rpx;
|
||
|
||
&:first-child {
|
||
margin-left: 0;
|
||
}
|
||
|
||
&--disabled {
|
||
color: #c0c4cc;
|
||
background-color: #fafafa;
|
||
}
|
||
}
|
||
}
|
||
</style>
|