当前位置:   article > 正文

Vue3后台管理系统(十八)分页组件_vue3实现分页组件

vue3实现分页组件

目录

一、场景效果

二、滑动工具类

三、组件封装

四、全局注册

五、使用案例


一、场景效果

 

二、滑动工具类

在src/utils文件夹下新建scroll-to.ts

  1. // src/utils/scroll-to.ts
  2. const easeInOutQuad = (t: number, b: number, c: number, d: number) => {
  3. t /= d / 2;
  4. if (t < 1) {
  5. return (c / 2) * t * t + b;
  6. }
  7. t--;
  8. return (-c / 2) * (t * (t - 2) - 1) + b;
  9. };
  10. // requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
  11. const requestAnimFrame = (function () {
  12. return (
  13. window.requestAnimationFrame ||
  14. (window as any).webkitRequestAnimationFrame ||
  15. (window as any).mozRequestAnimationFrame ||
  16. function (callback) {
  17. window.setTimeout(callback, 1000 / 60);
  18. }
  19. );
  20. })();
  21. /**
  22. * Because it's so fucking difficult to detect the scrolling element, just move them all
  23. * @param {number} amount
  24. */
  25. const move = (amount: number) => {
  26. document.documentElement.scrollTop = amount;
  27. (document.body.parentNode as HTMLElement).scrollTop = amount;
  28. document.body.scrollTop = amount;
  29. };
  30. const position = () => {
  31. return (
  32. document.documentElement.scrollTop ||
  33. (document.body.parentNode as HTMLElement).scrollTop ||
  34. document.body.scrollTop
  35. );
  36. };
  37. /**
  38. * @param {number} to
  39. * @param {number} duration
  40. * @param {Function} callback
  41. */
  42. export const scrollTo = (to: number, duration: number, callback?: any) => {
  43. const start = position();
  44. const change = to - start;
  45. const increment = 20;
  46. let currentTime = 0;
  47. duration = typeof duration === 'undefined' ? 500 : duration;
  48. const animateScroll = function () {
  49. // increment the time
  50. currentTime += increment;
  51. // find the value with the quadratic in-out easing function
  52. const val = easeInOutQuad(currentTime, start, change, duration);
  53. // move the document.body
  54. move(val);
  55. // do the animation unless its over
  56. if (currentTime < duration) {
  57. requestAnimFrame(animateScroll);
  58. } else {
  59. if (callback && typeof callback === 'function') {
  60. // the animation is done so lets callback
  61. callback();
  62. }
  63. }
  64. };
  65. animateScroll();
  66. };

三、组件封装

在src/components文件夹下新建Pagination文件夹,并在Pagination文件夹下新建index.vue

  1. <!--src/components/Pagination/index.vue-->
  2. <template>
  3. <div :class="{ hidden: hidden }" class="pagination-container">
  4. <el-pagination
  5. :background="background"
  6. v-model:current-page="currentPage"
  7. v-model:page-size="pageSize"
  8. :layout="layout"
  9. :page-sizes="pageSizes"
  10. :total="total"
  11. @size-change="handleSizeChange"
  12. @current-change="handleCurrentChange"
  13. />
  14. </div>
  15. </template>
  16. <script setup lang="ts">
  17. import { computed, PropType } from 'vue';
  18. import { scrollTo } from '@/utils/scroll-to';
  19. const props = defineProps({
  20. total: {
  21. required: true,
  22. type: Number as PropType<number>,
  23. default: 0
  24. },
  25. page: {
  26. type: Number,
  27. default: 1
  28. },
  29. limit: {
  30. type: Number,
  31. default: 20
  32. },
  33. pageSizes: {
  34. type: Array as PropType<number[]>,
  35. default() {
  36. return [10, 20, 30, 50];
  37. }
  38. },
  39. layout: {
  40. type: String,
  41. default: 'total, sizes, prev, pager, next, jumper'
  42. },
  43. background: {
  44. type: Boolean,
  45. default: true
  46. },
  47. autoScroll: {
  48. type: Boolean,
  49. default: true
  50. },
  51. hidden: {
  52. type: Boolean,
  53. default: false
  54. }
  55. });
  56. const emit = defineEmits(['update:page', 'update:limit', 'pagination']);
  57. const currentPage = computed<number | undefined>({
  58. get: () => props.page,
  59. set: value => {
  60. emit('update:page', value);
  61. }
  62. });
  63. const pageSize = computed<number | undefined>({
  64. get() {
  65. return props.limit;
  66. },
  67. set(val) {
  68. emit('update:limit', val);
  69. }
  70. });
  71. function handleSizeChange(val: number) {
  72. emit('pagination', { page: currentPage, limit: val });
  73. if (props.autoScroll) {
  74. scrollTo(0, 800);
  75. }
  76. }
  77. function handleCurrentChange(val: number) {
  78. currentPage.value = val;
  79. emit('pagination', { page: val, limit: props.limit });
  80. if (props.autoScroll) {
  81. scrollTo(0, 800);
  82. }
  83. }
  84. </script>
  85. <style scoped>
  86. .pagination-container {
  87. background: #fff;
  88. padding: 32px 16px;
  89. }
  90. .pagination-container.hidden {
  91. display: none;
  92. }
  93. </style>

四、全局注册

  1. // src/main.ts
  2. import Pagination from '@/components/Pagination/index.vue';
  3. app
  4. .component('Pagination', Pagination)
  5. .mount('#app')

五、使用案例

  1. <!--src/App.vue-->
  2. <script setup lang="ts">
  3. import Pagination from '@/components/Pagination/index.vue'
  4. import {onMounted, reactive} from "vue";
  5. const state = reactive({
  6. loading: true,
  7. queryParams: {
  8. pageNum: 1,
  9. pageSize: 10
  10. },
  11. dataList: [] as Data[],
  12. total: 0,
  13. });
  14. interface Data {
  15. id: number
  16. name: string
  17. }
  18. function handleQuery() {
  19. state.loading = true;
  20. state.dataList = [
  21. {id: 1,name: '张三'},
  22. {id: 2, name: '李四'},
  23. ];
  24. state.total = 1000;
  25. state.loading = false;
  26. }
  27. onMounted(()=>{
  28. handleQuery()
  29. })
  30. </script>
  31. <template>
  32. <div>
  33. <div v-for="data in state.dataList" :key="data.id">
  34. <div>
  35. 编码:{{ data.id }}
  36. </div>
  37. <div>
  38. 姓名:{{ data.name }}
  39. </div>
  40. </div>
  41. <div>
  42. <pagination
  43. v-if="state.total > 0"
  44. :total="state.total"
  45. v-model:page="state.queryParams.pageNum"
  46. v-model:limit="state.queryParams.pageSize"
  47. @pagination="handleQuery"
  48. />
  49. </div>
  50. </div>
  51. </template>
  52. <style>
  53. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/76859
推荐阅读
相关标签
  

闽ICP备14008679号