vue实现分页组件

父组件

<template>
  <div>
      <div class="log-bottom">
        <Pager
          ref="pager"
          :pageSize="pageSize"
          :curPage="curPage"
          :total="total"
          @setPage="gotoPage"
          @setRowNum="changeRowNum"
        />
      </div>
  </div>
</template>

<script>
import Pager from "page/pagesButton";
export default {
  data() {
    return {
      curPage: 1, //当前页
      total: "", //总共页数
      pageSize: 10, //每页记录数
    };
  },
  components: {
    Pager
  },
  methods: {
    initPage(page) {
      let url =  `http://10.1.0.117:5000/admin/log/index?page=${page}&pagesize=${this.pageSize}`;
      this.$axios.get(url).then(res => {
        this.log_list = res.data.data.logs;
        this.total = res.data.data.pagecount;
        console.log(res);
      });
    },
    gotoPage(page) {
      this.initPage(page)
      console.log(page);
    },
  },
  created() {
    this.initPage();
  }
};
</script>

分页组件,引入即可使用

<template>
  <div class="pager">
    <span v-if="showNextPage">共计{{totals}}条记录</span>
    <span v-if="showNextPage">{{Page}}/{{ Math.ceil(totals/rowNum)}}</span>
    <a href="javascript:void(0);" v-if="showFirstPage" @click="firstPage">首页</a>
    <a href="javascript:void(0);" v-if="showPrevPage" @click="prevPage">上一页</a>
    <a href="javascript:void(0);" v-if="showNextPage" @click="nextPage">下一页</a>
    <a href="javascript:void(0);" v-if="showLastPage" @click="lastPage">尾页</a>
    <div class="goto">
      <input type="text" v-model="gotoPage" />
      <a href="javascript:void(0);" @click="gotoNextPage">跳转</a>
    </div>
  </div>
</template>
<script>
export default {
  name: "Pager",
  props: ["pageSize", "curPage", "total"],
  data() {
    return {
      Page: this.curPage,
      totals: this.total,
      gotoPage: "",
      showFirstPage: true,
      showLastPage: true,
      showPrevPage: true,
      showNextPage: true,
      rowNum: this.pageSize
    };
  },
  methods: {
    nextPage() {
      //下一页
      if (this.Page <= this.totals / this.rowNum) {
        this.$emit("setPage", this.Page++ + 1); //调用父组件方法
        console.log(this.Page, "+++");
      }
    },
    prevPage() {
      //上一页
      if (this.Page >= 2) {
        this.$emit("setPage", this.Page-- - 1); //调用父组件方法
        console.log(this.Page, "-----");
      }
    },
    gotoNextPage() {
      //跳转页面
      if (this.gotoPage && /[1-9][0-9]*/.test(this.gotoPage)) {
        var pg = parseInt(this.gotoPage);
        if (pg <= Math.ceil(this.totals / this.rowNum) && pg > 0) {
          this.$emit("setPage", pg); //调用父组件方法
          this.Page = this.gotoPage;
        } else {
          this.gotoPage = "";
        }
      } else {
        this.gotoPage = "";
      }
    },
    firstPage() {
      this.Page = 1;
      this.$emit("setPage", 1); //调用父组件方法
    },
    lastPage() {
      this.Page = Math.ceil(this.totals / this.rowNum);
      this.$emit("setPage", this.Page); //调用父组件方法
    },
    initPager() {
      //不想写了,根据需求渲染dom
      this.showNextPage = true;
      this.showNextPage = true;
    }
  },
  watch: {
    //监控rowNum
    total(a, b) {
      if (a != "") {
        this.totals = a;
      }
    },
    curPage(a, b) {
      if (a != "") {
        this.Page = a;
      }
    },
    pageSize(a, b) {
      if (a != "") {
        this.rowNum = a;
      }
    }
  }
};
</script>
<style scoped>
.pager > span,
.pager > a,
.pager > select,
.pager > div {
  float: left;
  margin-left: 10px;
  font-size: 1.5vh;
  color: #7792ad;
}

.pager {
  width: 600px;
  margin: 0 auto;
  margin-top: 10px;
  height: 48px;
  /* background: #888; */
  line-height: 48px;
  margin-bottom: 40px;
}

.pager input,
.pager select {
  height: 40px;
  line-height: 40px;
  outline: none;
  border: 1px solid #888;
  padding: 10px;
  box-sizing: border-box;
}
.pager input {
  height: 20px;
  width: 20px;
 padding: 0;
  margin-right:10px; 
}
.pager .goto {
  margin-left: 10px;
}
.pager a {
  color: #7792ad;
  font-weight: bold;
  text-decoration: none;
}
.pager a:hover {
  color: skyblue;
}
</style>

完结撒花!!!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值