index.vue 2.61 KB
<script setup lang="ts">
/**
 * 分页
 * @module Pagination
 */
import { computed } from "vue";
import { scrollTo } from "@/utils/scroll";

// TIP 每个属性都必传,不带默认值
// defineProps<{
//   total: number;
//   currentPage: number;
//   pageSizes: number[];
//   pageSize: number;
// }>();

/**
 * @description 组件属性
 * @prop {number} total 内容总条数
 * @prop {number[] | null} pageSizes 每页显示个数选择器的选项设置
 * @prop {number | null} pageSize 当前每页内容条数
 * @prop {number | null} currentPage 当前页码
 * @prop {string | null} tagName 标签
 */
// TIP 可以带默认值
interface Props {
  total: number;
  pageSizes?: number[];
  pageSize?: number;
  currentPage?: number;
  tagName?: string;
}
const props = withDefaults(defineProps<Props>(), {
  total: 0,
  pageSizes:()=> [10, 20, 50, 100],
  pageSize: 10,
  currentPage: 1,
});

const emit = defineEmits<{
  (e: "pageChange", curPage: number, curSize: number, tagName: string): void;
  (e: "update:currentPage", val:number): void;
  (e: "update:pageSize", val:number): void;
}>();

const currentPage = computed({
  get: () => props.currentPage,
  set: val => {
    emit("update:currentPage", val);
  }
})
const pageSize = computed({
  get: () => props.pageSize,
  set: val => {
    emit("update:pageSize", val);
  }
})
// /**
//  * 监听当前分页改变
//  */
// const updateCurrentPage = (val) => {
//   console.log(val);
//   emit("update:currentPage", val);
// };
// /**
//  * 监听个数改变
//  */
// const updatePageSize = (val) => {
//   console.log(val);
//   emit("update:pageSize", val);
// };

/**
 * @method handleSizeChange
 * @description 每页内容条数变化响应
 */
const handleSizeChange = (val) => {
  scrollTo(0, 800);
  emit("pageChange", props.currentPage, val, props.tagName);
};
/**
 * @method handleCurrentChange
 * @description 当前页码变化响应
 */
const handleCurrentChange = (val) => {
  scrollTo(0, 800);
  emit("pageChange", val, props.pageSize, props.tagName);
};
</script>

<template>
  <div class="pagination-container">
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:pageSize="pageSize"
      :page-sizes="props.pageSizes"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
      :onUpdate:currentPage="currentPage"
      :onUpdate:pageSize="pageSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<style scoped lang="scss">
.pagination-container {
  padding: 10px;
}
.el-pagination {
  margin-top: 30px;
  text-align: right;
}
</style>