-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination-input.js
232 lines (194 loc) · 8.33 KB
/
pagination-input.js
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
/**
* Sometimes for quick navigation, it can be useful to allow an end user to
* enter which page they wish to jump to manually. This paging control uses a
* text input box to accept new paging numbers (arrow keys are also allowed
* for), and four standard navigation buttons are also presented to the end
* user.
*
* @name Navigation with text input
* @summary Shows an input element into which the user can type a page number
* @author [Allan Jardine](http://sprymedia.co.uk)
* @author [Gordey Doronin](http://github.com/GDoronin)
* @author [Benyamin Limanto](https://benyamin.xyz)
*
* @example
* $(document).ready(function() {
* $('#example').dataTable( {
* "pagingType": "input"
* } );
* } );
*/
(function ($) {
function calcDisableClasses(oSettings) {
var start = oSettings._iDisplayStart;
var length = oSettings._iDisplayLength;
var visibleRecords = oSettings.fnRecordsDisplay();
var all = length === -1;
// Gordey Doronin: Re-used this code from main jQuery.dataTables source code. To be consistent.
var page = all ? 0 : Math.ceil(start / length);
var pages = all ? 1 : Math.ceil(visibleRecords / length);
var disableFirstPrevClass = (page > 0 ? '' : oSettings.oClasses.sPageButtonDisabled);
var disableNextLastClass = (page < pages - 1 ? '' : oSettings.oClasses.sPageButtonDisabled);
return {
'first': disableFirstPrevClass,
'previous': disableFirstPrevClass,
'next': disableNextLastClass,
'last': disableNextLastClass
};
}
function calcCurrentPage(oSettings) {
return Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1;
}
function calcPages(oSettings) {
return Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength);
}
var firstClassName = 'first';
var previousClassName = 'previous';
var nextClassName = 'next';
var lastClassName = 'last';
var paginateClassName = 'paginate';
var paginatePageClassName = 'paginate_page';
var paginateInputClassName = 'paginate_input';
var paginateTotalClassName = 'paginate_total';
$.fn.dataTableExt.oPagination.input = {
'fnInit': function (oSettings, nPaging, fnCallbackDraw) {
var nFirst = document.createElement('li');
var nPrevious = document.createElement('li');
var nNext = document.createElement('li');
var nLast = document.createElement('li');
var nInput = document.createElement('input');
var nTotal = document.createElement('li');
var nInfo = document.createElement('li');
var language = oSettings.oLanguage.oPaginate;
var classes = oSettings.oClasses;
var info = language.info || 'Page _INPUT_ of _TOTAL_';
nFirst.innerHTML = '<a href="#'+oSettings.nTable.id+'" aria-controls="'+oSettings.nTable.id+'" data-dt-idx="0" tabindex="0" class="page-link">'+ language.sFirst + '</a>';
nPrevious.innerHTML = '<a href="#'+oSettings.nTable.id+'" aria-controls="'+oSettings.nTable.id+'" data-dt-idx="1" tabindex="0" class="page-link">'+ language.sPrevious + '</a>';
nNext.innerHTML = '<a href="#'+oSettings.nTable.id+'" aria-controls="'+oSettings.nTable.id+'" data-dt-idx="2" tabindex="0" class="page-link">'+ language.sNext + '</a>';
nLast.innerHTML = '<a href="#'+oSettings.nTable.id+'" aria-controls="'+oSettings.nTable.id+'" data-dt-idx="3" tabindex="0" class="page-link">'+ language.sLast + '</a>';
nFirst.className = firstClassName + ' ' + classes.sPageButton;
nPrevious.className = previousClassName + ' ' + classes.sPageButton;
nNext.className = nextClassName + ' ' + classes.sPageButton;
nLast.className = lastClassName + ' ' + classes.sPageButton;
nInput.className = paginateInputClassName + " form-control";
nInput.style.maxWidth = "100px";
nTotal.className = paginateTotalClassName + " py-2 pr-2";
if (oSettings.sTableId !== '') {
nPaging.setAttribute('id', oSettings.sTableId + '_' + paginateClassName);
nFirst.setAttribute('id', oSettings.sTableId + '_' + firstClassName);
nPrevious.setAttribute('id', oSettings.sTableId + '_' + previousClassName);
nNext.setAttribute('id', oSettings.sTableId + '_' + nextClassName);
nLast.setAttribute('id', oSettings.sTableId + '_' + lastClassName);
}
nInput.type = 'text';
info = info.replace(/_INPUT_/g, '</li>' + nInput.outerHTML + '<li class=p-2>');
info = info.replace(/_TOTAL_/g, '</li>' + nTotal.outerHTML + '<li>');
nInfo.innerHTML = '<li class=p-2>' + info + '</li>';
console.log(info);
nContainer = $("<ul class='pagination'></ul>").get(0);
nContainer.appendChild(nFirst);
nContainer.appendChild(nPrevious);
$(nInfo).children().each(function (i, n) {
nContainer.appendChild(n);
});
nContainer.appendChild(nNext);
nContainer.appendChild(nLast);
nPaging.appendChild(nContainer);
$(nFirst).click(function() {
var iCurrentPage = calcCurrentPage(oSettings);
if (iCurrentPage !== 1) {
oSettings.oApi._fnPageChange(oSettings, 'first');
fnCallbackDraw(oSettings);
}
});
$(nPrevious).click(function() {
var iCurrentPage = calcCurrentPage(oSettings);
if (iCurrentPage !== 1) {
oSettings.oApi._fnPageChange(oSettings, 'previous');
fnCallbackDraw(oSettings);
}
});
$(nNext).click(function() {
var iCurrentPage = calcCurrentPage(oSettings);
if (iCurrentPage !== calcPages(oSettings)) {
oSettings.oApi._fnPageChange(oSettings, 'next');
fnCallbackDraw(oSettings);
}
});
$(nLast).click(function() {
var iCurrentPage = calcCurrentPage(oSettings);
if (iCurrentPage !== calcPages(oSettings)) {
oSettings.oApi._fnPageChange(oSettings, 'last');
fnCallbackDraw(oSettings);
}
});
$(nPaging).find('.' + paginateInputClassName).keyup(function (e) {
// 38 = up arrow, 39 = right arrow
if (e.which === 38 || e.which === 39) {
this.value++;
}
// 37 = left arrow, 40 = down arrow
else if ((e.which === 37 || e.which === 40) && this.value > 1) {
this.value--;
}
if (this.value === '' || this.value.match(/[^0-9]/)) {
/* Nothing entered or non-numeric character */
this.value = this.value.replace(/[^\d]/g, ''); // don't even allow anything but digits
return;
}
var iNewStart = oSettings._iDisplayLength * (this.value - 1);
if (iNewStart < 0) {
iNewStart = 0;
}
if (iNewStart >= oSettings.fnRecordsDisplay()) {
iNewStart = (Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength;
}
oSettings._iDisplayStart = iNewStart;
fnCallbackDraw(oSettings);
});
// Take the brutal approach to cancelling text selection.
$('span', nPaging).bind('mousedown', function () { return false; });
$('span', nPaging).bind('selectstart', function() { return false; });
// If we can't page anyway, might as well not show it.
var iPages = calcPages(oSettings);
if (iPages <= 1) {
$(nPaging).hide();
}
},
'fnUpdate': function (oSettings) {
if (!oSettings.aanFeatures.p) {
return;
}
var iPages = calcPages(oSettings);
var iCurrentPage = calcCurrentPage(oSettings);
var an = oSettings.aanFeatures.p;
if (iPages <= 1) // hide paging when we can't page
{
$(an).hide();
return;
}
var disableClasses = calcDisableClasses(oSettings);
$(an).show();
// Enable/Disable `first` button.
$(an).children("ul.pagination").children('.' + firstClassName)
.removeClass(oSettings.oClasses.sPageButtonDisabled)
.addClass(disableClasses[firstClassName]);
// Enable/Disable `prev` button.
$(an).children("ul.pagination").children('.' + previousClassName)
.removeClass(oSettings.oClasses.sPageButtonDisabled)
.addClass(disableClasses[previousClassName]);
// Enable/Disable `next` button.
$(an).children("ul.pagination").children('.' + nextClassName)
.removeClass(oSettings.oClasses.sPageButtonDisabled)
.addClass(disableClasses[nextClassName]);
// Enable/Disable `last` button.
$(an).children("ul.pagination").children('.' + lastClassName)
.removeClass(oSettings.oClasses.sPageButtonDisabled)
.addClass(disableClasses[lastClassName]);
// Paginate of N pages text
$(an).find('.' + paginateTotalClassName).html(iPages);
// Current page number input value
$(an).find('.' + paginateInputClassName).val(iCurrentPage);
}
};
})(jQuery);