Blame view

src/Pagination/Pagination.js 5.65 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,
80abca23   Imshann   feat(pagination):...
20
          showTotal: Function,
3a3ecabe   Imshann   init
21
22
23
24
25
26
27
28
      };
  
      state = {
          total: null,
          current: null,
          pageSize: null,
          pageNum: null,
          pageNumList: null,
4b23b387   Imshann   feat(pagination):...
29
          defaultPageSize: this.props.defaultPageSize || 10
3a3ecabe   Imshann   init
30
31
      };
  
80abca23   Imshann   feat(pagination):...
32
      watch = {
5b248282   Imshann   优化
33
34
          total: function (newVal) {
              if (newVal) {
80abca23   Imshann   feat(pagination):...
35
                  this.state.total = Number(newVal);
5b248282   Imshann   优化
36
                  this.reset();
80abca23   Imshann   feat(pagination):...
37
38
39
40
41
42
                  this.state.pageNum = this.getPageNum();
                  this.state.pageNumList = this.getPageNumList();
              }
          }
      }
  
3a3ecabe   Imshann   init
43
      constructor() {
dd962f77   Imshann   优化
44
          esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
45
          this.state.total = Number(this.props.total || 0);
061629e7   Imshann   add
46
47
48
49
50
          this.state.current = Number(
              this.props.current || this.props.defaultCurrent || 1
          );
          this.state.pageSize =
              this.props.pageSize || this.props.defaultPageSize || 10;
3a3ecabe   Imshann   init
51
          this.state.pageNum = this.getPageNum();
061629e7   Imshann   add
52
53
54
          this.state.pageNumList = this.getPageNumList();
      }
  
80abca23   Imshann   feat(pagination):...
55
56
57
58
59
60
      handleShowTotal() {
          return this.props.showTotal({
              total: this.state.total
          })
      }
  
061629e7   Imshann   add
61
62
63
64
65
66
67
68
69
70
71
72
73
      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";
          }
      }
  
5b248282   Imshann   优化
74
75
76
77
      reset() {
          this.state.current = 1;
      }
  
061629e7   Imshann   add
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      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
116
117
118
      }
  
      getPageNum() {
061629e7   Imshann   add
119
120
121
          return (
              Math.ceil(
                  this.state.total /
fe4cf8e2   Imshann   修复BUG
122
                      (this.state.pageSize || this.props.defaultPageSize || 10)
061629e7   Imshann   add
123
124
125
126
127
128
              ) || 1
          );
      }
  
      getPopupContainer() {
          return $element[0].querySelector(".ant-pagination-options");
3a3ecabe   Imshann   init
129
130
131
132
133
134
      }
  
      handleNext() {
          if (this.state.current === this.state.pageNum) {
              return false;
          }
5b248282   Imshann   优化
135
          this.handleClick(this.state.current+1);
3a3ecabe   Imshann   init
136
137
      }
  
3a3ecabe   Imshann   init
138
139
140
141
      handlePrev() {
          if (this.state.current === 1) {
              return false;
          }
5b248282   Imshann   优化
142
          this.handleClick(this.state.current-1);
3a3ecabe   Imshann   init
143
144
145
      }
  
      handleClick(value) {
e7610b68   Imshann   feat(pagination):...
146
147
148
          if (value === this.state.current) {
              return;
          }
061629e7   Imshann   add
149
150
151
152
153
154
155
          if (value === "next") {
              value = this.state.current + 5;
          }
          if (value === "prev") {
              value = this.state.current - 5;
          }
          this.setCurrent(this.getCurrent(value));
3a3ecabe   Imshann   init
156
157
158
159
      }
  
      handleChange() {
          let current = this.state.current;
fe4cf8e2   Imshann   修复BUG
160
161
          this.state.pageNum = this.getPageNum();
          this.state.pageNumList = this.getPageNumList();
3a3ecabe   Imshann   init
162
163
164
165
166
167
168
          if (this.state.current > this.state.pageNum) {
              current = this.state.pageNum;
          } else if (this.state.current < 1) {
              current = 1;
          }
          this.props.onChange({
              page: current,
e7610b68   Imshann   feat(pagination):...
169
              pageSize: parseInt(this.state.pageSize),
3a3ecabe   Imshann   init
170
171
172
173
174
175
176
177
          });
      }
  
      handleSelectChange(value) {
          this.state.current = 1;
          this.state.pageSize = parseInt(value);
          this.handleChange();
      }
061629e7   Imshann   add
178
  
3a3ecabe   Imshann   init
179
180
181
182
183
184
185
186
187
188
      getCurrent(number) {
          if (number > this.state.pageNum) {
              return this.state.pageNum;
          }
          if (number < 1) {
              return 1;
          }
          return parseInt(number);
      }
  
1b6f912f   Imshann   优化
189
      setCurrent(value) {
3a3ecabe   Imshann   init
190
191
192
193
          if (!value) {
              return;
          }
          this.state.current = this.getCurrent(value);
061629e7   Imshann   add
194
          this.state.pageNumList = this.getPageNumList();
3a3ecabe   Imshann   init
195
          this.handleChange();
1b6f912f   Imshann   优化
196
197
198
199
      }
  
      handleBlur(event) {
          this.setCurrent(event.target.value);
3a3ecabe   Imshann   init
200
201
          event.target.value = null;
      }
1b6f912f   Imshann   优化
202
203
204
205
206
207
208
  
      onKeyPress(event) {
          if (event.keyCode === 13 || event.keyCode === 32) {
              this.setCurrent(event.target.value);
              event.target.value = null;
          }
      }
3a3ecabe   Imshann   init
209
  }