Blame view

src/Pagination/Pagination.js 5.06 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
  
  class Pagination {
dd962f77   Imshann   优化
5
6
      useModules = ["esNgAntd"];
  
3a3ecabe   Imshann   init
7
8
9
10
11
12
13
14
15
16
17
18
      template = template;
  
      props = {
          defaultCurrent: Number,
          current: Number,
          total: Number,
          defaultPageSize: Number,
          pageSize: Number,
          onChange: Function,
          onShowSizeChange: Function,
          showQuickJumper: Boolean,
          showSizeChanger: Boolean,
710b4ac0   Imshann   优化
19
          size: String,
3a3ecabe   Imshann   init
20
21
22
23
24
25
26
27
28
29
30
      };
  
      state = {
          total: null,
          current: null,
          pageSize: null,
          pageNum: null,
          pageNumList: null,
      };
  
      constructor() {
dd962f77   Imshann   优化
31
          esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
32
          this.state.total = Number(this.props.total || 0);
061629e7   Imshann   add
33
34
35
36
37
          this.state.current = Number(
              this.props.current || this.props.defaultCurrent || 1
          );
          this.state.pageSize =
              this.props.pageSize || this.props.defaultPageSize || 10;
3a3ecabe   Imshann   init
38
          this.state.pageNum = this.getPageNum();
061629e7   Imshann   add
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
          this.state.pageNumList = this.getPageNumList();
      }
  
      getItemLinkClassName(value) {
          if (typeof value === "number") {
              return (
                  "ant-pagination-item" +
                  (this.state.current === value
                      ? " ant-pagination-item-active"
                      : "")
              );
          } else {
              return "ant-pagination-jump-next ant-pagination-jump-next-custom-icon";
          }
      }
  
      getPageNumList() {
          let pageNumList = [this.state.current];
          let pageNum = this.getPageNum();
          if (pageNum <= 7 || this.state.current - 1 < 4) {
              for (let i = this.state.current - 1; i > 0; i--) {
                  pageNumList.unshift(i);
              }
          } else {
              let len = this.state.current - 1 > 2 ? 2 : this.state.current - 1;
              for (let i = 1; i <= len; i++) {
                  pageNumList.unshift(this.state.current - i);
              }
              if (this.state.current - 2 > 2) {
                  pageNumList.unshift("prev");
                  pageNumList.unshift(1);
              }
          }
  
          if (pageNum <= 7 || pageNum - this.state.current < 4) {
              for (let i = this.state.current + 1; i <= pageNum; i++) {
                  pageNumList.push(i);
              }
          } else {
              let limit =
                  3 - this.state.current > 0 ? 2 + (3 - this.state.current) : 2;
              let len =
                  pageNum - this.state.current > limit
                      ? limit
                      : pageNum - this.state.current;
              for (let i = 1; i <= len; i++) {
                  pageNumList.push(this.state.current + i);
              }
              if (pageNum - this.state.current > 2) {
                  pageNumList.push("next");
                  pageNumList.push(pageNum);
              }
          }
          return pageNumList;
3a3ecabe   Imshann   init
93
94
95
      }
  
      getPageNum() {
061629e7   Imshann   add
96
97
98
99
100
101
102
103
104
105
          return (
              Math.ceil(
                  this.state.total /
                      (this.props.pageSize || this.props.defaultPageSize || 10)
              ) || 1
          );
      }
  
      getPopupContainer() {
          return $element[0].querySelector(".ant-pagination-options");
3a3ecabe   Imshann   init
106
107
108
109
110
111
112
113
114
      }
  
      handleNext() {
          if (this.state.current === this.state.pageNum) {
              return false;
          }
          this.handleClick(++this.state.current);
      }
  
3a3ecabe   Imshann   init
115
116
117
118
119
120
121
122
      handlePrev() {
          if (this.state.current === 1) {
              return false;
          }
          this.handleClick(--this.state.current);
      }
  
      handleClick(value) {
061629e7   Imshann   add
123
124
125
126
127
128
129
          if (value === "next") {
              value = this.state.current + 5;
          }
          if (value === "prev") {
              value = this.state.current - 5;
          }
          this.setCurrent(this.getCurrent(value));
3a3ecabe   Imshann   init
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
          // 更新回调
          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();
      }
061629e7   Imshann   add
155
  
3a3ecabe   Imshann   init
156
157
158
159
160
161
162
163
164
165
      getCurrent(number) {
          if (number > this.state.pageNum) {
              return this.state.pageNum;
          }
          if (number < 1) {
              return 1;
          }
          return parseInt(number);
      }
  
1b6f912f   Imshann   优化
166
      setCurrent(value) {
3a3ecabe   Imshann   init
167
168
169
170
          if (!value) {
              return;
          }
          this.state.current = this.getCurrent(value);
061629e7   Imshann   add
171
          this.state.pageNumList = this.getPageNumList();
3a3ecabe   Imshann   init
172
          this.handleChange();
1b6f912f   Imshann   优化
173
174
175
176
      }
  
      handleBlur(event) {
          this.setCurrent(event.target.value);
3a3ecabe   Imshann   init
177
178
          event.target.value = null;
      }
1b6f912f   Imshann   优化
179
180
181
182
183
184
185
  
      onKeyPress(event) {
          if (event.keyCode === 13 || event.keyCode === 32) {
              this.setCurrent(event.target.value);
              event.target.value = null;
          }
      }
3a3ecabe   Imshann   init
186
  }