ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vue 分页 pagination

2020-03-19 14:02:22  阅读:367  来源: 互联网

标签:em count pagination vue 分页 text list background page


<template>
  <div class="pagination_box">
    <div class="pagination_wrap">
      <ul class="clearfix">

        <!-- prev -->
        <li class="first btn" :class="{'disabled': current === 1}" @click="setCurrent(current - 1)">
          <a href="javascript:;"><em></em></a>
        </li>

        <!-- 页数列表 -->
        <li v-for="(item, index) in grouplist" :key="index">
          <a href="javascript:;" @click="setCurrent(item.val)" :class="{'active': current == item.val}">{{item.text}}</a>
        </li>

        <!-- next -->
        <li class="last btn" :class="{'disabled': current === page}" @click="setCurrent(current + 1)">
          <a href="javascript:;"><em></em></a>
        </li>

      </ul>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'pagination',
    props: {
      // 当前页码
      currentPage: {
        type: Number,
        default: 1
      },
      // 每页显示条数
      display: {
        type: Number,
        default: 10
      },
      // 总页数
      total: {
        type: Number,
        default: 0
      },
      // 分页条数
      pagegroup: {
        type: Number,
        default: 5,
        coerce: function (v) {
          v = v > 0 ? v : 5;
          return v % 2 === 1 ? v : v + 1;
        }
      }
    },
    data() {
      return {
        pages: null
      }
    },
    computed: {
      /**
       * 返回当前页码
       */
      current() {
        return this.currentPage
      },
      /**
       * 显示总页数
       */
      page() {
        return Math.ceil(this.total / this.display)
      },
      /**
       * 获取分页页码
       */
      grouplist() {
        var len = this.page,
          temp = [],
          list = [],
          count = Math.floor(this.pagegroup / 2),
          center = this.current;
        if (len <= this.pagegroup) {
          while (len--) {
            temp.push({
              text: this.page - len,
              val: this.page - len
            });
          };
          return temp;
        }
        while (len--) {
          temp.push(this.page - len);
        };
        var idx = temp.indexOf(center);
        (idx < count) && (center = center + count - idx);
        (this.current > this.page - count) && (center = this.page - count);
        temp = temp.splice(center - count - 1, this.pagegroup);
        do {
          var t = temp.shift();
          list.push({
            text: t,
            val: t
          });
        } while (temp.length);
        if (this.page > this.pagegroup) {
          (this.current > count + 1) && list.unshift({
            text: '...',
            val: list[0].val - 1
          });
          (this.current < this.page - count) && list.push({
            text: '...',
            val: list[list.length - 1].val + 1
          });
        }
        return list;
      }
    },
    methods: {
      /**
       * 设置当前页
       * @param {String} num  // 当前页数
       */
      setCurrent(num) {
        let idx = parseFloat(num)
        // 如果是点击当前页, 当前页面大于0, 小于总页数
        if (this.current !== idx && idx > 0 && idx < this.page + 1) {
          this.$emit("change", idx)
          this.pages = null
        }
      }
    },
  }
</script>
<style lang="stylus">
  .pagination_box {
  padding: 0 10px;
  media-style('padding', (0 10px) (0 20px) (0 20px) (0 30px));
  text-align: right;

  .pagination_wrap {
    display: inline-block;
    padding: 10px 0;

    media-style('padding', (10px 0) (12px 0) (16px 0) (22px 0));

    ul {
      li {
        float: left;
        position: relative;
        width: 24px;
        height: 24px;
        margin: 0 1px;
        media-style('margin', (0 2px) (0 4px) (0 4px) (0 6px))

        a {
          display: block;
          font-size: 12px;
          text-align: center;
          line-height: 24px;
          position: relative;
          border: 1px solid transparent;
          color: #000000;
          transition();

          &:hover,
          &.active {
            color: $base-button-background;
            border-color: $base-button-background;
          }
        }

        &.btn {
          a {
            height: 100%;

            &:hover {
              em {
                background: $base-button-background;
              }
            }

            em {
              position: absolute;
              left: 50%;
              top: 50%;
              width: 6px;
              height: 6px;
              background: #000000;
              transition();

              &:after {
                content: '';
                position: absolute;
                left: 50%;
                top: 50%;
                width: 6px;
                height: 6px;
                margin: -4px 0 0 -2px;
                background: #ffffff;
                transform: rotate(0deg);
              }
            }
          }
        }

        &.first {
          em {
            margin: -3px 0 0 -2px;
            transform: rotate(45deg);
          }
        }

        &.last {
          em {
            margin: -3px 0 0 -4px;
            transform: rotate(225deg);
          }
        }

        &.btn.disabled {
          em {
            background: #C1C6CD;
          }

          a {
            &:hover {
              color: #C1C6CD;
              border-color: transparent;

              em {
                background: #C1C6CD;
              }
            }
          }
        }
      }

    }

  }
}
</style>

  

标签:em,count,pagination,vue,分页,text,list,background,page
来源: https://www.cnblogs.com/alantao/p/12523949.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有