wxvirus wxvirus
首页
  • Go文章

    • Go语言学习
  • Rust

    • Rust学习
  • Java

    • 《Java》
  • Python文章

    • Python
  • PHP文章

    • PHP设计模式
  • 学习笔记

    • 《Git》
  • HTML
  • CSS
  • JS
  • 技术文档
  • GitHub技巧
  • 刷题
  • 博客搭建
  • 算法学习
  • 架构设计
  • 设计模式
  • 学习
  • 面试
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

无解的lifecycle

let today = new Beginning()
首页
  • Go文章

    • Go语言学习
  • Rust

    • Rust学习
  • Java

    • 《Java》
  • Python文章

    • Python
  • PHP文章

    • PHP设计模式
  • 学习笔记

    • 《Git》
  • HTML
  • CSS
  • JS
  • 技术文档
  • GitHub技巧
  • 刷题
  • 博客搭建
  • 算法学习
  • 架构设计
  • 设计模式
  • 学习
  • 面试
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • HTML

  • CSS

  • JS

  • flutter

  • vue3

    • vuecli多环境配置
    • 全局使用antdv图标组件
    • 集成富文本插件wangeditor
    • vue分页组件
      • 进行使用
    • 登录页开发
    • 后台布局开发
    • vue3配合vite初始化项目的一些配置
  • 特效

  • framework

  • 页面
  • vue3
wxvirus
2022-05-12

vue分页组件

这里的分页样式和 bootstrap 有关,样式可以自己改,这个无所谓

pagination 组件

<template>
    <div class="pagination" role="group" aria-label="分页">
        <button type="button" class="btn btn-default btn-white btn-round"
                v-bind:disabled="page === 1"
                v-on:click="selectPage(1)">
            1
        </button>
        <button type="button" class="btn btn-default btn-white btn-round"
                v-bind:disabled="page === 1"
                v-on:click="selectPage(page - 1)">
            上一页
        </button>
        <button v-for="p in pages" v-bind:id="'page-' + p"
                type="button" class="btn btn-default btn-white btn-round"
                v-bind:class="{'btn-primary active':page == p}"
                v-on:click="selectPage(p)">
            {{ p }}
        </button>
        <button type="button" class="btn btn-default btn-white btn-round"
                v-bind:disabled="page === pageTotal"
                v-on:click="selectPage(page + 1)">
            下一页
        </button>
        <button type="button" class="btn btn-default btn-white btn-round"
                v-bind:disabled="page === pageTotal"
                v-on:click="selectPage(pageTotal)">
            {{ pageTotal || 1 }}
        </button>

        <span class="m-padding-10">
            每页
            <select v-model="size">
                <option value="1">1</option>
                <option value="5">5</option>
                <option value="10">10</option>
                <option value="20">20</option>
                <option value="50">50</option>
                <option value="100">100</option>
            </select>
            条,共【{{ total }}】条
        </span>
    </div>
</template>

<script>
  export default {
    name: "pagination",
    props: {
      list: {
        type: Function,
        default: null
      },
      // 显示的页码数,比如总归有100页,只显示10页,其他用省略号表示
      itemCount: Number
    },
    data() {
      return {
        // 总行数
        total: 0,
        // 每页条数
        size: 10,
        // 当前页码
        page: 0,
        // 总页数
        pageTotal: 0,
        // 显示的页码数组
        pages: [],
      }
    },
    methods: {
      /**
       * 渲染分页组件
       * @param page
       * @param total
       */
      render(page, total) {
        let _this = this;
        _this.page = page;
        _this.total = total;
        _this.pageTotal = Math.ceil(total / _this.size);
        _this.pages = _this.getPageItems(_this.pageTotal, page, _this.itemCount || 5);
      },
      /**
       * 查询某一页
       * @param page
       */
      selectPage(page) {
        let _this = this;
        if (page < 1) {
          page = 1;
        }
        if (page > _this.pageTotal) {
          page = _this.pageTotal;
        }
        if (this.page !== page) {
          _this.page = page;
          if (_this.list) {
            _this.list(page);
          }
        }
      },
      /**
       * 当前显示再页面上的页码
       * @param total
       * @param current
       * @param length
       * @returns {Array}
       */
      getPageItems(total, current, length) {
        let items = [];
        if (length >= total) {
          for (let i = 1; i <= total; i++) {
            items.push(i);
          }
        } else {
          let base = 0;
          // 前移
          if (current - 0 > Math.floor((length - 1) / 2)) {
            // 后移
            base = Math.min(total, current - 0 + Math.ceil((length - 1) / 2)) - length;
          }
          for (let i = 1; i <= length; i++) {
            items.push(base + i);
          }
        }
        return items;
      }
    }
  }
</script>

<style scoped>
    .pagination {
        vertical-align: middle !important;
        font-size: 16px;
        margin-top: 0;
        margin-bottom: 10px;
    }

    .pagination button {
        margin-right: 5px;
    }

    .btn-primary.active {
        background-color: #2f7bba !important;
        border-color: #27689d !important;
        color: white !important;
        font-weight: 600;
    }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

# 进行使用

在页面中进行使用该组件

<template>
    <div>
        <pagination ref="pagination" v-bind:list="list"></pagination>
    </div>
</template>

<script>
    import Pagination from "组件所在的位置";
    export default {
        name: "该页面名称",
        components: {Pagination}
    }
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13

这里的list,是当前页面的list方法,使用axios对后端进行获取数据 使用post方法 参数: page、size > page:当前页码 size:每页条数

组件也可以绑定itemCount来初始化页面显示多少个页码数

<pagination ref='pagination' v-bind:list='list' v-bind:itemCount='8'></pagination>
1

这里的页码数主要是为了假如你想分 100 页,总不能在页面上显示 100 个页码数吧,这样就很不美观

编辑 (opens new window)
上次更新: 2022/05/12, 22:16:08
集成富文本插件wangeditor
登录页开发

← 集成富文本插件wangeditor 登录页开发→

最近更新
01
vue3配合vite初始化项目的一些配置
07-26
02
网盘系统开发学习
07-24
03
linux多进程
06-19
更多文章>
Theme by Vdoing | Copyright © 2021-2024 wxvirus 苏ICP备2021007210号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式