-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator.component.ts
233 lines (213 loc) · 5.77 KB
/
paginator.component.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Component, Input, Output, EventEmitter } from '@angular/core';
import * as _ from 'lodash';
@Component({
selector: 'paginator',
templateUrl: './paginator.component.html',
styles: [`
.paginatorBox {
padding: .25em .5em;
border: 1px solid #d5d5d5;
}
.currentRow {
width: 20%;
float: left;
text-align: center;
}
.currentPages {
width: 20%;
float: left;
text-align: center;
}
.currentPages-input {
border: 1px solid #d5d5d5;
padding: 0 5px;
border-radius: 2px;
outline: none;
position: relative;
top: -1.5px;
width: 2rem;
text-align: right;
}
.paginator {
width: 30%;
float: left;
text-align: center;
}
.paginator a {
color: #000;
border: 1px solid #d5d5d5;
border-radius: 2px;
margin-right: 10px;
padding: 1px 6px;
font-size: 12px;
cursor: pointer;
}
.paginator a.pageDisable{
opacity: 0.3;
cursor: default;
}
.totalRecords {
width: 30%;
float: right;
text-align: center;
}
.currentPages-select {
border: 1px solid #d5d5d5;
padding: 0 5px;
border-radius: 2px;
outline: none;
position: relative;
top: -1.5px;
text-align: center;
}
`],
})
export class PaginatorComponent {
// 组件样式 ==> 用来外部添加样式
@Input() paginatorStyle: any;
// 选中数据 ==> 用来判断多少行
@Input() selectedData: any;
// 数据总条数 ==> 用来显示数据总过有多少页
@Input() totalRecords: any;
// select数组 用来展示每页默认多少条
@Input() rowsPerPageOptions: any;
// 分页点击事件
@Output() onPageChange = new EventEmitter<any>();
// 当前选中了多少行
currentRow: number = 0;
// 当前总页数 - 传进来的多少总条数 / 传进来下拉框的值
totalPages: number;
// 共多少行 - 传进来的总条数
totalRow: number;
// 当前第几页
currentPages: number = 1;
// 默认展示下拉框第一条
currentPagesSelect: any;
// 临时储存页数,方便输入跳转
tempPage: any = 1;
paginatorData: any = [
{ icon: "fa fa-step-backward", pageDisable: true, value: "firstPage" },
{ icon: "fa fa-backward", pageDisable: true, value: "prePage" },
{ icon: "fa fa-forward", pageDisable: false, value: "nextPage" },
{ icon: "fa fa-step-forward", pageDisable: false, value: "lastPage" },
];
constructor() { }
ngOnInit() {
}
ngOnChanges() {
if (this.rowsPerPageOptions) {
this.currentPagesSelect = this.rowsPerPageOptions[0];
this.totalRow = this.totalRecords;
this.totalPages = Math.ceil(this.totalRecords / this.rowsPerPageOptions[0]);
this.onPageChange.emit({
currentPage: this.tempPage,
pageCount: this.totalPages,
pageRows: this.currentPagesSelect,
});
}
if (this.selectedData) {
if (Array.isArray(this.selectedData)) {
this.currentRow = this.selectedData.length;
} else {
this.currentRow = 1;
}
} else {
this.currentRow = 0;
}
}
// 下拉框
selectFn() {
this.currentPagesSelect = this.currentPagesSelect * 1;
this.totalPages = Math.ceil(this.totalRecords / this.currentPagesSelect);
if (this.tempPage >= this.totalPages) {
this.tempPage = this.currentPages = 1;
}
this.onPageChange.emit({
currentPage: this.tempPage,
pageCount: this.totalPages,
pageRows: this.currentPagesSelect,
});
this.onPageClickFn(this.paginatorData);
}
// 分页点击
onPageClick(e) {
if (!e.pageDisable) {
if (e.value === "nextPage") {
if (this.tempPage != this.totalPages) {
this.tempPage += 1;
}
}
if (e.value === "prePage") {
if (this.tempPage != 1) {
this.tempPage -= 1;
}
}
if (e.value === "firstPage") {
this.tempPage = 1;
}
if (e.value === "lastPage") {
this.tempPage = this.totalPages;
}
this.currentPages = this.tempPage;
this.onPageChange.emit({
currentPage: this.tempPage,
pageCount: this.totalPages,
pageRows: this.currentPagesSelect,
});
this.onPageClickFn(this.paginatorData);
}
}
// 分页点击方法
onPageClickFn(paginatorData) {
paginatorData.forEach(el => {
el.pageDisable = false;
if (this.tempPage == 1) {
if (el.value == "firstPage" || el.value == "prePage") {
el.pageDisable = true;
}
}
if (this.tempPage == this.totalPages) {
if (el.value == "nextPage" || el.value == "lastPage") {
el.pageDisable = true;
}
}
});
}
// ------------------- beg pageInput 方法 -------------------
pageInputkeydown(e) {
if (this.keydownCode(e)) {
if (e.key === ".") {
return false;
}
if (isNaN(e.key * 1)) {
return false;
}
if (e.code === "Space") {
return false;
}
}
}
pageInputkeyup(e) {
e.target.value = e.target.value.replace(/[^\d+-.]/g, '');
if (e.key == "Enter") {
this.tempPage = this.currentPages = e.target.value;
if (e.target.value * 1 >= this.totalPages) {
e.target.value = this.tempPage = this.currentPages = this.totalPages;
}
if (e.target.value * 1 <= 1) {
e.target.value = this.tempPage = this.currentPages = 1;
}
this.tempPage = this.tempPage * 1;
this.onPageChange.emit({
currentPage: this.tempPage,
pageCount: this.totalPages,
pageRows: this.currentPagesSelect,
});
this.onPageClickFn(this.paginatorData);
}
}
keydownCode(e) {
return e.key !== "Backspace" && e.key !== "ArrowLeft" && e.key !== "ArrowRight" && e.key !== "ArrowUp" && e.key !== "ArrowDown";
}
// ------------------- end pageInput 方法 -------------------
}