Pagination.js
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
102
103
104
105
106
import template from "./Pagination.html";
class Pagination {
template = template;
props = {
defaultCurrent: Number,
current: Number,
total: Number,
defaultPageSize: Number,
pageSize: Number,
onChange: Function,
onShowSizeChange: Function,
showQuickJumper: Boolean,
showSizeChanger: Boolean,
};
state = {
total: null,
current: null,
pageSize: null,
pageNum: null,
pageNumList: null,
};
constructor() {
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);
}
handleBlur(event) {
let value = event.target.value;
if (!value) {
return;
}
this.state.current = this.getCurrent(value);
this.handleChange();
event.target.value = null;
}
}