Blame view

src/Pagination/Pagination.js 3 KB
3a3ecabe   Imshann   init
1
  import template from "./Pagination.html";
dd962f77   Imshann   优化
2
  import style from "antd/lib/pagination/style/index.css";
3a3ecabe   Imshann   init
3
4
5
  
  class Pagination {
  
dd962f77   Imshann   优化
6
7
      useModules = ["esNgAntd"];
  
3a3ecabe   Imshann   init
8
9
10
11
12
13
14
15
16
17
18
19
      template = template;
  
      props = {
          defaultCurrent: Number,
          current: Number,
          total: Number,
          defaultPageSize: Number,
          pageSize: Number,
          onChange: Function,
          onShowSizeChange: Function,
          showQuickJumper: Boolean,
          showSizeChanger: Boolean,
710b4ac0   Imshann   优化
20
          size: String,
3a3ecabe   Imshann   init
21
22
23
24
25
26
27
28
29
30
31
      };
  
      state = {
          total: null,
          current: null,
          pageSize: null,
          pageNum: null,
          pageNumList: null,
      };
  
      constructor() {
dd962f77   Imshann   优化
32
          esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
          this.state.total = Number(this.props.total || 0);
          this.state.current = Number(this.props.current || this.props.defaultCurrent || 1);
          this.state.pageSize = this.props.pageSize || this.props.defaultPageSize || 10;
          this.state.pageNum = this.getPageNum();
          this.state.pageNumList = Array(this.state.pageNum)
              .fill(0)
              .map((v, i) => i + 1);
      }
  
      getPageNum() {
          return Math.ceil(this.state.total / (this.props.pageSize || this.props.defaultPageSize || 10)) || 1;
      }
  
      handleNext() {
          if (this.state.current === this.state.pageNum) {
              return false;
          }
          this.handleClick(++this.state.current);
      }
  
      getPopupContainer() {
          return $element[0].querySelector(".ant-pagination-options");
      }
  
      handlePrev() {
          if (this.state.current === 1) {
              return false;
          }
          this.handleClick(--this.state.current);
      }
  
      handleClick(value) {
          this.state.current = value;
          // 更新回调
          this.props.onChange({
              page: this.state.current,
              pageSize: this.state.pageSize,
          });
      }
  
      handleChange() {
          let current = this.state.current;
          if (this.state.current > this.state.pageNum) {
              current = this.state.pageNum;
          } else if (this.state.current < 1) {
              current = 1;
          }
          this.props.onChange({
              page: current,
              pageSize: this.state.pageSize,
          });
      }
  
      handleSelectChange(value) {
          this.state.current = 1;
          this.state.pageSize = parseInt(value);
          this.handleChange();
      }
      
      getCurrent(number) {
          if (number > this.state.pageNum) {
              return this.state.pageNum;
          }
          if (number < 1) {
              return 1;
          }
          return parseInt(number);
      }
  
1b6f912f   Imshann   优化
102
      setCurrent(value) {
3a3ecabe   Imshann   init
103
104
105
106
107
          if (!value) {
              return;
          }
          this.state.current = this.getCurrent(value);
          this.handleChange();
1b6f912f   Imshann   优化
108
109
110
111
      }
  
      handleBlur(event) {
          this.setCurrent(event.target.value);
3a3ecabe   Imshann   init
112
113
          event.target.value = null;
      }
1b6f912f   Imshann   优化
114
115
116
117
118
119
120
  
      onKeyPress(event) {
          if (event.keyCode === 13 || event.keyCode === 32) {
              this.setCurrent(event.target.value);
              event.target.value = null;
          }
      }
3a3ecabe   Imshann   init
121
  }