解决分页器跳转页数跨度大而引起的请求数据量大的问题

爱被打了一巴掌 2022-12-03 01:59 21阅读 0赞

实现效果:仅一页时不显示分页器,页数固定只显示十个数字,页数跳转固定最多只能跨5页跳转

在这里插入图片描述

  • 封装分页器组件


    共 {
    { total }} 条

    < 上一页

    {
    { item }}

    下一页 >

    export default {
    name: ‘Index’,
    props: {

    1. total: {
    2. type: Number,
    3. default: 0
    4. },
    5. pageSize: {
    6. type: Number,
    7. default: 20
    8. },
    9. pageNum: {
    10. type: Number,
    11. default: 1
    12. },
    13. currPage: {
    14. type: Number,
    15. default: 0
    16. }

    },
    data() {

    1. return {
    2. page: this.pageNum || 1,
    3. pageCount: 1,
    4. pageList: undefined
    5. }

    },
    watch: {

    1. page(newValue, oldValue) {
    2. if (this.pageCount <= 10) {
    3. this.pageList = this.pageCount
    4. } else if (this.pageCount > 10 && newValue > 5) {
    5. this.pageList = []
    6. if (newValue + 4 > this.pageCount) {
    7. for (let i = 0; i < 10; i++) {
    8. this.pageList.unshift(this.pageCount - i)
    9. }
    10. } else {
    11. for (let i = newValue - 5; i <= newValue + 4; i++) {
    12. this.pageList.push(i)
    13. }
    14. }
    15. } else {
    16. this.pageList = 10
    17. }
    18. window.scrollTo({
    19. top: 0,
    20. left: 0,
    21. behavior: 'smooth'
    22. })
    23. },
    24. total: {
    25. handler: function(newValue) {
    26. this.pageCount = newValue % this.pageSize === 0 ? this.total / this.pageSize : Math.floor(this.total / this.pageSize) + 1
    27. this.pageList = this.pageCount <= 10 ? this.pageCount : 10
    28. }
    29. }

    },
    methods: {

    1. pageChange(d) {
    2. this.page = d
    3. this.$emit('current-change', d)
    4. },
    5. pre() {
    6. if (this.page > 1) {
    7. this.page--
    8. this.pageChange(this.page)
    9. }
    10. },
    11. next() {
    12. if (this.page < this.pageCount) {
    13. this.page++
    14. this.pageChange(this.page)
    15. }
    16. }

    }
    }

    .do-pagination{
    font-size: 14px;
    font-weight: 500;

    1. padding: 14px 15px;
    2. /*background-color: #f5f5f5;*/

    white-space: nowrap;
    user-select: none; span{

    1. display: inline-block;
    2. margin-right: 12px;
    3. text-align: center;
    4. vertical-align: middle;
    5. line-height: 36px;
    6. border: 0;
    7. border-radius: 6px;
    8. background-color: #fff;
    9. color: #409EFF;

    }

    1. .n{
    2. width: 80px;
    3. padding: 0;
    4. line-height: 36px;
    5. border: 0;
    6. border-radius: 6px;
    7. background-color: #fff;
    8. cursor: pointer; &.disable{
    9. cursor: not-allowed;
    10. color: inherit;
    11. }
    12. }
    13. .p{
    14. padding: 0 12px;
    15. min-width: 36px;
    16. height: 36px;
    17. transition: all 0.32s ease; &.active{
    18. background: #409EFF;
    19. color: #fff;
    20. }
    21. }

    .t{

    1. padding: 0 12px;
    2. min-width: 36px;
    3. height: 36px;
    4. font-size: 13px;
    5. font-weight: 400;
    6. color: #606266;
    7. background-color: unset;

    }
    }

为了方便使用,将封装好的组件挂载在全局

  • 在工具utils中引入

    import DoPagination from ‘@/components/DoPagination’
    export default (Vue)=>{
    Vue.component(‘DoPagination’,DoPagination)
    }

  • 挂载全局

    import components from ‘@/utils/components’
    Vue.use(components)

  • 引用

发表评论

表情:
评论列表 (有 0 条评论,21人围观)

还没有评论,来说两句吧...

相关阅读