wangrong
2021-11-24 130b72d6c5734f8acaac0214b6dfe5c8bfa319c3
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
<template>
  <el-pagination
    style="text-align: center"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="page"
    :page-sizes="size_list"
    :page-size="size"
    background
    :pager-count="pagerCount"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  >
  </el-pagination>
</template>
 
<script>
export default {
  data() {
    return {
      size_list: [10, 20, 30, 40],
      total: 0,
      page: 1,
      size: 10,
      pagerCount: 5,
    };
  },
  created() {
    this.getTotalsize();
  },
  props: ["totalCountOrURL", "params"],
  watch: {
    totalCountOrURL() {
      this.getTotalsize();
    },
    params() {
      this.getTotalsize();
    },
  },
  methods: {
    getTotalsize() {
      if (typeof this.totalCountOrURL == "number") {
        // 为number表示前台分页 值即为总数  不需要从后台查询
        this.total = this.totalCountOrURL;
      } else {
        // 参数若没有初始化 则不向后台发送请求 (应避免参数本身就为null的情况)
        for (let p in this.params) {
          if (!this.params[p]) {
            return;
          }
        }
        this.$getJSON(this.totalCountOrURL, this.params).then((res) => {
          this.total = res.data[0].count;
          this.$emit("totalCount", this.total);
        });
      }
    },
    handleSizeChange(a) {
      this.size = a;
      if (this.page > Math.ceil(this.total / this.size)) {
        this.page = Math.ceil(this.total / this.size);
      }
      this.$emit("change", {
        page: this.page,
        size: this.size,
      });
    },
    handleCurrentChange(a) {
      this.page = a;
      this.$emit("change", {
        page: this.page,
        size: this.size,
      });
    },
  },
};
</script>