介绍一下,已经有很多的vue分页的组件了,大家都是大同小易,那么我就结合自身的使用,写出了一片文章

  • 首先在新建一个分页模块

  1. 在模块中引入相应的代码,(内有详细的注释)
  2. template中

     <div class="page-bar">
    <ul>
    <li class="first">
    <span>共{{dataNum}}条记录 第 {{cur}} / {{all}} 页</span>
    </li>
    <li v-if="cur>1">
    <a v-on:click="cur--,pageClick()"><</a>//点击上一页
    </li>
    <li v-if="cur==1">
    <a class="banclick"><</a>//点击第一页时显示
    </li>
    <li class="li_a" v-for="index in indexs" v-bind:class="{ 'active': cur == index}">
    <a v-on:click="btnClick(index)">{{ index }}</a>//页码
    </li>
    <li v-if="cur!=all">
    <a v-on:click="cur++,pageClick()">></a>//点击下一页
    </li>
    <li v-if="cur == all">
    <a class="banclick">></a> //点击最后一页时显示
    </li>
    <li class="last_li">
    <span>共<i>{{all}}</i>页</span> // 共有多少页
    </li>
    </ul>
    </div>
  3. style中的内容

     .page-bar {
    text-align: center;
    width: 100%;
    height: 36px;
    margin: 0 auto;
    position: relative;
    } .page-bar ul {
    min-width: 700px;
    display: block;
    overflow: hidden;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    } .page-bar li {
    display: block;
    width: 36px;
    height: 36px;
    border-radius: 4px;
    list-style: none;
    overflow: hidden;
    position: relative;
    float: left;
    margin-left: 8px;
    }
    .page-bar .first{
    display: block;
    width: 170px;
    height: 36px;
    font-size: 14px;
    line-height: 36px;
    text-align: center;
    }
    .page-bar .last_li{
    width: 85px;
    height: 36px;
    border: 1px solid #ddd;
    }
    .page-bar .last_li span{
    width: 100%;
    height: 100%;
    line-height: 36px;
    text-align: center;
    float: left;
    }
    .page-bar li:first-child {
    margin-left: 0px
    } .page-bar a {
    width: 34px;
    height: 34px;
    border: 1px solid #ddd;
    text-decoration: none;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    /*margin-left: -1px;*/
    line-height: 34px;
    color: #333;
    cursor: pointer
    } .page-bar .li_a a:hover {
    background-color: #eee;
    border: 1px solid #40A9FF;
    color: #40A9FF;
    } .page-bar a.banclick {
    cursor: not-allowed;
    } .page-bar .active a {
    color: #fff;
    cursor: default;
    background-color: #1890FF;
    border-color: #1890FF;
    } .page-bar i {
    font-style: normal;
    color: #d44950;
    margin: 0px 4px;
    font-size: 14px;
    }
  4. script

    export default {
    //显示的声明组件
    name: "paging",
    //从父级组件中传值过来的,你可以自己设置名字,但是需要跟父级传入的名字一致!
    props : ["dataAll","dataCur","datanum","dataDatanum"], data() {
    return {
    all: this.dataAll, //总页数
    cur: this.dataCur ,//当前页码
    num: this.datanum , //一页显示的数量 奇数
    dataNum: this.dataDatanum,//数据的数量 } },
    watch: {
    cur: function(oldValue, newValue) {
    //父组件通过change方法来接受当前的页码
    this.$emit('change', oldValue)
    //这里是直接点击执行函数
    }
    },
    methods: {
    btnClick: function(data) { //页码点击事件
    if(data != this.cur) {
    this.cur = data
    }
    },
    pageClick: function() {
    console.log('现在在' + this.cur + '页');
    //父组件通过change方法来接受当前的页码
    //这里是点击下一页执行函数
    this.$emit('change', this.cur)
    }
    }, computed: {
    indexs: function() {
    var left = 1;
    var right = this.all;
    var ar = [];
    if(this.all >= this.num ) {
    if(this.cur > 3 && this.cur < this.all - 2) {
    left = this.cur - (this.num-1)/2
    right = this.cur + (this.num-1)/2
    } else {
    if(this.cur <= 3) {
    left = 1
    right = this.num
    } else {
    right = this.all
    left = this.all - (this.num - 1);
    }
    }
    }
    while(left <= right) {
    ar.push(left)
    left++
    }
    return ar
    } } }
  5. 父级的组件内容

    <template>
    //这是我自己设置的,可以根据情况不用设置不同的样式
    <div class="page">
    //这里时通过props传值到子级,并有一个回调change的函数,来获取自己传值到父级的值
    <paging :dataAll="all" :dataCur="cur" :datanum="num" :dataDatanum="dataNum" @change="pageChange"></paging>
    </div>
    </template>
    <style scoped>
    .page {
    width: 100%;
    min-width: 1068px;
    height: 36px;
    margin: 40px auto;
    }
    </style>
    <script>
    import Paging from './paging'
    export default {
    name: "homepage",
    components: {
    Paging
    },
    data() {
    return {
    all: 40, //总页数
    cur: 1, //当前页码
    num: 7, //一页显示的数量 必须是奇数
    dataNum: 400, //数据的数量
    }
    },
    methods: {
    //子级传值到父级上来的动态拿去
    pageChange: function(page) {
    this.cur = page
    }
    }
    }
    </script>
  6. 最后重新保存,重新运行

       npm run dev

  1. 注意
    1.可以根据自己喜好来自己动手做一个分页,我在其它人的基础之上添加了页码以及当前页面数,也可以添加跳转的页数(暂时没有做),也可以更改css样式来改变!
    2.本人才疏学浅,请大家多多包涵!

vue中的自定义分页插件组件的更多相关文章

  1. MyBatis-编写自定义分页插件

    一.基础知识 本文测试和源码分析参考版本: Mybatis-version:3.5.5 本文相关测试源代码:https://github.com/wuya11/mybatis_demo 1.1 参考方 ...

  2. vue中可以自定义动画的前缀

    vue中可以自定义动画的前缀1.只需在中加入name属性即可 <transition name="my"> <h6 v-if="flag2"& ...

  3. 在vue中使用Element的message组件

    在vue中使用Element的message组件 在vue文件中使用 this.$message({ message: "提示信息", type: "success&qu ...

  4. Vue中利用$emit实现子组件向父组件通信

    Vue中利用$emit实现子组件向父组件通信 父组件 <template> <div> <p>我是父组件</p> <child :isShow=& ...

  5. 如何在实际项目中使用PageHelper分页插件

    PageHelper是一个分页插件,能够简单快速的帮助开发人员完成常见的分页功能,你只需要简单的使用两行代码就可以完成一个分页效果- 最近做一个科创项目,使用Maven+SSM的环境,有分页的功能,于 ...

  6. vue中创建全局单文件组件/命令

    1.在 vue中如果我们使用基于vue.js编写的插件,我们可以使用Vue.use() 如在main.js中: 2.添加全局命令,让每个vue单文件组件都可以使用到: 第一步:最好建一个全局的命令文件 ...

  7. 在vue中创建自定义指令

    原文:https://dev.to/ratracegrad/creating-custom-directives-in-vue-58hh 翻译:心上有杨 指令是带有 v- 前缀的特殊属性.指令的作用是 ...

  8. MyBatis拦截器自定义分页插件实现

    MyBaits是一个开源的优秀的持久层框架,SQL语句与代码分离,面向配置的编程,良好支持复杂数据映射,动态SQL;MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyB ...

  9. vue中使用better-scroll滚动条插件

    应用场景: overflow: hidden会让超出的部分隐藏,并且无法拖拽,所以可使用插件让长列表限定的区域滚动拖拽. 参考:https://zhuanlan.zhihu.com/p/2740702 ...

随机推荐

  1. kubectl格式化输出和调试

    1.格式化输出 以特定的格式想终端输出详细信息,可以在 kubectl 命令中添加 -o  或者 -output 选项 输出格式 描述 -o=custom-columns=<spec> 使 ...

  2. Android recycleview item水波纹效果

    item的xml 根标签下添加如下三个属性 android:clickable="true" android:focusable="true" android: ...

  3. Mysql Sql 语句练习题 (50道)

    MySql 语句练习50题 表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_ ...

  4. [LC] 149. Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  5. HotSpot Java对象创建,内存布局以及访问方式

    内存中对象的创建.对象的结构以及访问方式. 一.对象的创建 在语言层面上,对象的创建只不过是一个new关键字而已,那么在虚拟机中又是一个怎样的过程呢? (一)判断类是否加载.虚拟机遇到一条new指令的 ...

  6. HDU-2138-How many prime numbers(Miller-Rabin新解法)

    题目传送门 sol1:普通判到sqrt(n)的素数判定,不多说了. 素数判定 #include "bits/stdc++.h" using namespace std; bool ...

  7. jQuery插件开发小结

    jQuery插件开发规范 1. 使用闭包 (function($) { // Code goes here })(jQuery); 这是来自jQuery官方的插件开发规范要求,使用这种编写方式有什么好 ...

  8. [LC] 122. Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. JavaScript学习总结(七)Ajax和Http状态字

    转自:http://segmentfault.com/a/1190000000691919 Ajax及其工作原理 AJAX 是一种与服务器交换数据无需刷新网页的技术,最早由Google公司在谷歌地图里 ...

  10. JavaScript学习总结(四)function函数部分

    转自:http://segmentfault.com/a/1190000000660786 概念 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. js 支持两种函数:一类是语言内部的函数 ...