157 lines
4.6 KiB
Vue
157 lines
4.6 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* shared/components/PaginationBar.vue
|
||
* 用途:通用偏移分页条(摘要、每页条数、页码按钮)。
|
||
* 依赖:buildOffsetPageButtons、common.pagination i18n、components-ds 样式类。
|
||
* 逻辑:父组件 v-model:page / v-model:page-size,服务端或客户端分页均可复用。
|
||
*/
|
||
import { computed } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { buildOffsetPageButtons } from '@/shared/composables/useOffsetPagination';
|
||
|
||
interface Props {
|
||
total: number;
|
||
page: number;
|
||
pageSize: number;
|
||
/** 服务端已算好的总页数;不传则按 total/pageSize 推算 */
|
||
pages?: number;
|
||
pageSizeOptions?: number[];
|
||
/** 为 false 时强制隐藏;默认 total > 0 时显示 */
|
||
visible?: boolean;
|
||
}
|
||
|
||
const props = withDefaults(defineProps<Props>(), {
|
||
pages: 0,
|
||
pageSizeOptions: () => [10, 20, 50, 100],
|
||
visible: undefined,
|
||
});
|
||
|
||
const emit = defineEmits<{
|
||
(e: 'update:page', value: number): void;
|
||
(e: 'update:pageSize', value: number): void;
|
||
}>();
|
||
|
||
const { t } = useI18n();
|
||
|
||
const totalPages = computed(() => {
|
||
if (props.pages > 0) return props.pages;
|
||
const size = Math.max(1, Number(props.pageSize) || 20);
|
||
const total = Math.max(0, Number(props.total) || 0);
|
||
return Math.max(1, Math.ceil(total / size));
|
||
});
|
||
|
||
const safePage = computed(() =>
|
||
Math.min(Math.max(1, Number(props.page) || 1), totalPages.value),
|
||
);
|
||
|
||
const pageButtons = computed(() => buildOffsetPageButtons(safePage.value, totalPages.value));
|
||
|
||
const showBar = computed(() => {
|
||
if (props.visible !== undefined) return props.visible;
|
||
return props.total > 0;
|
||
});
|
||
|
||
/**
|
||
* 跳转到指定页并通知父组件。
|
||
* @param target - 目标页码(1-based)
|
||
*/
|
||
const onGoToPage = (target: number): void => {
|
||
const next = Math.min(Math.max(1, target), totalPages.value);
|
||
emit('update:page', next);
|
||
};
|
||
|
||
/**
|
||
* 上一页并通知父组件。
|
||
*/
|
||
const onPrevPage = (): void => {
|
||
onGoToPage(safePage.value - 1);
|
||
};
|
||
|
||
/**
|
||
* 下一页并通知父组件。
|
||
*/
|
||
const onNextPage = (): void => {
|
||
onGoToPage(safePage.value + 1);
|
||
};
|
||
|
||
/**
|
||
* 每页条数变更时重置到第 1 页。
|
||
* @param size - 新的 pageSize
|
||
*/
|
||
const onPageSizeChange = (size: number): void => {
|
||
emit('update:pageSize', size);
|
||
emit('update:page', 1);
|
||
};
|
||
</script>
|
||
|
||
<template>
|
||
<div
|
||
v-if="showBar"
|
||
class="flex flex-col gap-3 rounded-2xl border border-default bg-surface px-4 py-3 md:flex-row md:items-center md:justify-between"
|
||
>
|
||
<div class="text-sm text-secondary">
|
||
{{ t('common.pagination.summary', { total, page: safePage, pages: totalPages }) }}
|
||
</div>
|
||
<div class="flex flex-wrap items-center gap-2">
|
||
<label class="flex items-center gap-2 text-xs text-secondary">
|
||
<span>{{ t('common.pagination.perPage') }}</span>
|
||
<span class="relative inline-flex items-center">
|
||
<select
|
||
:value="pageSize"
|
||
class="admin-table-filter-input appearance-none pl-2 pr-7 py-1 text-xs leading-5"
|
||
@change="onPageSizeChange(Number(($event.target as HTMLSelectElement).value))"
|
||
>
|
||
<option v-for="size in pageSizeOptions" :key="`page-size-${size}`" :value="size">
|
||
{{ size }}
|
||
</option>
|
||
</select>
|
||
<svg
|
||
class="pointer-events-none absolute right-2 h-3.5 w-3.5 text-muted"
|
||
viewBox="0 0 20 20"
|
||
fill="currentColor"
|
||
aria-hidden="true"
|
||
>
|
||
<path
|
||
fill-rule="evenodd"
|
||
d="M5.23 7.21a.75.75 0 0 1 1.06.02L10 11.168l3.71-3.938a.75.75 0 1 1 1.08 1.04l-4.25 4.5a.75.75 0 0 1-1.08 0l-4.25-4.5a.75.75 0 0 1 .02-1.06Z"
|
||
clip-rule="evenodd"
|
||
/>
|
||
</svg>
|
||
</span>
|
||
<span>{{ t('common.pagination.itemsUnit') }}</span>
|
||
</label>
|
||
<button
|
||
type="button"
|
||
:disabled="safePage <= 1"
|
||
class="admin-report-page-btn"
|
||
@click="onPrevPage"
|
||
>
|
||
{{ t('common.pagination.prevPage') }}
|
||
</button>
|
||
<button
|
||
v-for="btn in pageButtons"
|
||
:key="`page-btn-${btn}`"
|
||
type="button"
|
||
:disabled="btn === -1"
|
||
class="admin-report-page-btn"
|
||
:class="btn === -1
|
||
? 'border-transparent text-muted cursor-default opacity-100'
|
||
: btn === safePage
|
||
? 'admin-report-page-btn--active'
|
||
: ''"
|
||
@click="btn !== -1 && onGoToPage(btn)"
|
||
>
|
||
{{ btn === -1 ? '...' : btn }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
:disabled="safePage >= totalPages"
|
||
class="admin-report-page-btn"
|
||
@click="onNextPage"
|
||
>
|
||
{{ t('common.pagination.nextPage') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</template>
|