Blame view

src/Pagination/Pagination.js 5.59 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
33
34
35
36
37
38
39
40
41
      watch = {
          total: function (newVal, oldVal) {
              if (newVal && oldVal) {
                  this.state.total = Number(newVal);
                  this.state.pageNum = this.getPageNum();
                  this.state.pageNumList = this.getPageNumList();
              }
          }
      }
  
3a3ecabe   Imshann   init
42
      constructor() {
dd962f77   Imshann   优化
43
          esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
44
          this.state.total = Number(this.props.total || 0);
061629e7   Imshann   add
45
46
47
48
49
          this.state.current = Number(
              this.props.current || this.props.defaultCurrent || 1
          );
          this.state.pageSize =
              this.props.pageSize || this.props.defaultPageSize || 10;
3a3ecabe   Imshann   init
50
          this.state.pageNum = this.getPageNum();
061629e7   Imshann   add
51
52
53
          this.state.pageNumList = this.getPageNumList();
      }
  
80abca23   Imshann   feat(pagination):...
54
55
56
57
58
59
      handleShowTotal() {
          return this.props.showTotal({
              total: this.state.total
          })
      }
  
061629e7   Imshann   add
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
102
103
104
105
106
107
108
109
110
      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
111
112
113
      }
  
      getPageNum() {
061629e7   Imshann   add
114
115
116
          return (
              Math.ceil(
                  this.state.total /
fe4cf8e2   Imshann   修复BUG
117
                      (this.state.pageSize || this.props.defaultPageSize || 10)
061629e7   Imshann   add
118
119
120
121
122
123
              ) || 1
          );
      }
  
      getPopupContainer() {
          return $element[0].querySelector(".ant-pagination-options");
3a3ecabe   Imshann   init
124
125
126
127
128
129
130
131
132
      }
  
      handleNext() {
          if (this.state.current === this.state.pageNum) {
              return false;
          }
          this.handleClick(++this.state.current);
      }
  
3a3ecabe   Imshann   init
133
134
135
136
137
138
139
140
      handlePrev() {
          if (this.state.current === 1) {
              return false;
          }
          this.handleClick(--this.state.current);
      }
  
      handleClick(value) {
e7610b68   Imshann   feat(pagination):...
141
142
143
          if (value === this.state.current) {
              return;
          }
061629e7   Imshann   add
144
145
146
147
148
149
150
          if (value === "next") {
              value = this.state.current + 5;
          }
          if (value === "prev") {
              value = this.state.current - 5;
          }
          this.setCurrent(this.getCurrent(value));
3a3ecabe   Imshann   init
151
152
153
154
      }
  
      handleChange() {
          let current = this.state.current;
fe4cf8e2   Imshann   修复BUG
155
156
          this.state.pageNum = this.getPageNum();
          this.state.pageNumList = this.getPageNumList();
3a3ecabe   Imshann   init
157
158
159
160
161
162
163
          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):...
164
              pageSize: parseInt(this.state.pageSize),
3a3ecabe   Imshann   init
165
166
167
168
169
170
171
172
          });
      }
  
      handleSelectChange(value) {
          this.state.current = 1;
          this.state.pageSize = parseInt(value);
          this.handleChange();
      }
061629e7   Imshann   add
173
  
3a3ecabe   Imshann   init
174
175
176
177
178
179
180
181
182
183
      getCurrent(number) {
          if (number > this.state.pageNum) {
              return this.state.pageNum;
          }
          if (number < 1) {
              return 1;
          }
          return parseInt(number);
      }
  
1b6f912f   Imshann   优化
184
      setCurrent(value) {
3a3ecabe   Imshann   init
185
186
187
188
          if (!value) {
              return;
          }
          this.state.current = this.getCurrent(value);
061629e7   Imshann   add
189
          this.state.pageNumList = this.getPageNumList();
3a3ecabe   Imshann   init
190
          this.handleChange();
1b6f912f   Imshann   优化
191
192
193
194
      }
  
      handleBlur(event) {
          this.setCurrent(event.target.value);
3a3ecabe   Imshann   init
195
196
          event.target.value = null;
      }
1b6f912f   Imshann   优化
197
198
199
200
201
202
203
  
      onKeyPress(event) {
          if (event.keyCode === 13 || event.keyCode === 32) {
              this.setCurrent(event.target.value);
              event.target.value = null;
          }
      }
3a3ecabe   Imshann   init
204
  }