Blame view

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