element-ui封装分页组件

2019-11-12  本文已影响0人  嫑着急
<template>
  <div class="pagination">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 20, 30, 40, 50, 60, 80, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="currentPage"
      :page-size="limit"
      :total="total"
      :small="small">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    name: "pagination",
    data() {
      return {
        currentPage: 1
      }
    },
    props: {
      //总条数
      'total': {
        required: false,
        default: 0
      },
     // 没页显示多少数据
      'limit': {
        required: false,
        default: 10
      },
      //是否使用小型分页样式
      'small': {
        required: false,
        type: Boolean,
        default: false
      }
    },
    // watch: {
      // currentPage(val) {
        // 改变这个值并不会触发 handleCurrentChange
        // if (typeof val === "number") {
          // console.log('prop currentPage!!!');
          // this.currentPage = val;
        // }
      // },
    // },
    methods: {
      // 当前页变化
      handleCurrentChange(val) {
        this.$emit("handleCurrentChange", val);
      },
      // size变化
      handleSizeChange(val) {
        this.currentPage = 1;    //重置页码为1
        this.$emit('handleSizeChange', val);
      },
    }
  }
</script>

<style scoped>
  .pagination {
    margin: 20px 0;
    text-align: center;
  }
</style>
<pagination :currentPage="currentPage" :total="total" :limit="pagesize" :small="true"
     @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange"/>
import pagination from '../../components/Pagination.vue'
data() {
      return {
        currentPage: 1,
        pagesize: 10,
        total: 0
      }
 },
methods:{
        handleCurrentChange(val) {
           this.currentPage = val;
           this.All_data(this.currentPage,this.pagesize);
        },
        handleSizeChange(val) {
           this.pagesize = val;
           this.currentPage = 1;    //这里选择每页多少数据,重置页码为1
           this.All_data(this.currentPage,this.pagesize);
        },
        All_data(page,size){
         let data = {
           "page":page,
           "size":size
         }
         // this.tableData = []
         this.$axios.post(this.$url + '/master/comment/bypage',data)
           .then((res) => {
             // console.log(res.data.pojo.list)
             this.total = res.data.pojo.total
             this.tableData = res.data.pojo.list
           })
           .catch((res) => {
             console.log(res)
           })
       },
},
created() {
       this.All_data(this.currentPage,this.pagesize)
}

上一篇 下一篇

猜你喜欢

热点阅读