forked from denhartog/jquery-simple-pagination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-simple-pagination-plugin.js
303 lines (277 loc) · 11.8 KB
/
jquery-simple-pagination-plugin.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
(function($){
$.fn.simplePagination = function(options)
{
var settings = $.extend({}, $.fn.simplePagination.defaults, options);
/*
NUMBER FORMATTING
*/
function simple_number_formatter(number, digits_after_decimal, thousands_separator, decimal_separator)
{
//OTHERWISE 0==false==undefined
digits_after_decimal = isNaN(digits_after_decimal) ? 2 : parseInt(digits_after_decimal);
//was a thousands place separator provided?
thousands_separator = (typeof thousands_separator === 'undefined') ? ',' : thousands_separator;
//was a decimal place separator provided?
decimal_separator = (typeof decimal_separator === 'undefined') ? '.' : decimal_separator;
//123.45 => 123==integer; 45==fraction
var parts = ((+number).toFixed(digits_after_decimal) + '').split(decimal_separator), // Force Number typeof with +: +number
//obtain the integer part
integer = parts[0] + '',
//obtain the fraction part IF one exists
fraction = (typeof parts[1] === 'undefined') ? '' : parts[1],
//create the decimal(fraction) part of the answer
decimal = digits_after_decimal > 0 ? decimal_separator + fraction : '',
//find 1 or more digits, EXACTLY PRECEDING, exactly 3 digits
pattern = /(\d+)(\d{3})/;
//pattern = /(\d)(?=(\d{3})+$)/; .replace(..., '$1' + thousands_separator
//while the pattern can be matched
while(pattern.test(integer))
{
//insert the specified thousands place separator
integer = integer.replace(pattern, '$1' + thousands_separator + '$2');
}
//return the formated number!
return integer + decimal;
}
return this.each(function()
{
var container_id = '#' + $(this).attr('id'),
items = $(this).find(settings.pagination_container).children(),
item_count = items.length,
items_per_page = parseInt(settings.items_per_page),
page_count = Math.ceil(item_count / items_per_page),
number_of_visible_page_numbers = parseInt(settings.number_of_visible_page_numbers);
// Show the appropriate items given the specific page_number
function refresh_page(page_number, item_range_min, item_range_max)
{
items.hide();
items.slice(item_range_min, item_range_max).show();
}
function refresh_first(page_number)
{
// e.g.
// <a href="#" class="simple-pagination-navigation-first [simple-pagination-nagivation-disabled]"
// data-simple-pagination-page-number="1">First</a>
/*
var element = document.createElement(settings.navigation_element);
element.id = 'rawr';
element.href = '#';
element.className = settings.html_prefix + '-navigation-first';
element.className += page_count === 1 || page_number === 1 ? ' ' + settings.html_prefix + '-navigation-disabled' : '';
element.setAttribute('data-' + settings.html_prefix + '-page-number', 1);
element.appendChild(document.createTextNode(settings.first_content));
*/
var first_html = '<' + settings.navigation_element + ' href="#" class="' + settings.html_prefix + '-navigation-first';
first_html += page_count === 1 || page_number === 1 ? ' ' + settings.html_prefix + '-navigation-disabled' : '';
first_html += '" data-' + settings.html_prefix + '-page-number="' + 1 + '">' + settings.first_content + '</' + settings.navigation_element + '>';
return first_html; // return element.outerHTML;
}
function refresh_previous(page_number)
{
var previous_page = page_number > 1 ? page_number - 1 : 1,
previous_html = '<' + settings.navigation_element + ' href="#" class="' + settings.html_prefix + '-navigation-previous';
previous_html += page_count === 1 || page_number === 1 ? ' ' + settings.html_prefix + '-navigation-disabled' : '';
previous_html += '" data-' + settings.html_prefix + '-page-number="' + previous_page + '">' + settings.previous_content + '</' + settings.navigation_element + '>';
return previous_html;
}
function refresh_next(page_number)
{
var next_page = page_number + 1 > page_count ? page_count : page_number + 1,
next_html = '<' + settings.navigation_element + ' href="#" class="' + settings.html_prefix + '-navigation-next';
next_html += page_count === 1 || page_number === page_count ? ' ' + settings.html_prefix + '-navigation-disabled' : '';
next_html += '" data-' + settings.html_prefix + '-page-number="' + next_page + '">' + settings.next_content + '</' + settings.navigation_element + '>';
return next_html;
}
function refresh_last(page_number)
{
var last_html = '<' + settings.navigation_element + ' href="#" class="' + settings.html_prefix + '-navigation-last';
last_html += page_count === 1 || page_number === page_count ? ' ' + settings.html_prefix + '-navigation-disabled' : '';
last_html += '" data-' + settings.html_prefix + '-page-number="' + page_count + '">' + settings.last_content + '</' + settings.navigation_element + '>';
return last_html;
}
function refresh_page_numbers(page_number)
{
// half_of_number_of_page_numbers_visable causes even numbers to be treated the same as the next LOWEST odd number (e.g. 6 === 5)
// Used to center the current page number in 'else' below
var half_of_number_of_page_numbers_visable = Math.ceil(number_of_visible_page_numbers / 2) - 1,
current_while_page = 0,
page_numbers_html = [],
create_page_navigation = function()
{
page_number_html = '<' + settings.navigation_element + ' href="#" class="' + settings.html_prefix + '-navigation-page';
page_number_html += page_count === 1 || page_number === current_while_page ? ' ' + settings.html_prefix + '-navigation-disabled' : '';
page_number_html += '" data-' + settings.html_prefix + '-page-number="' + current_while_page + '">' + simple_number_formatter(current_while_page, 0, settings.thousands_separator) + '</' + settings.navigation_element + '>';
page_numbers_html.push(page_number_html);
};
//are we on the left half of the desired truncation length?
if(page_number <= half_of_number_of_page_numbers_visable)
{
var max = half_of_number_of_page_numbers_visable * 2 + 1;
max = max > page_count ? page_count : max;
while(current_while_page < max)
{
++current_while_page;
create_page_navigation();
}
}
//are we on the right side of the desired truncation length?
else if(page_number > page_count - half_of_number_of_page_numbers_visable)
{
var min = page_count - half_of_number_of_page_numbers_visable * 2 - 1;
current_while_page = min < 0 ? 0 : min;
while(current_while_page < page_count)
{
++current_while_page;
create_page_navigation();
}
}
//have lots of pages
//half_of_num... + number_of_visible_page_numbers + half_of_num...
//center the current page between: number_of_visible_page_numbers
else
{
var min = page_number - half_of_number_of_page_numbers_visable - 1,
max = page_number + half_of_number_of_page_numbers_visable;
current_while_page = min < 0 ? 0 : min;
max = max > page_count ? page_count : max;//shouldn't need this but just being cautious
while(current_while_page < max)
{
++current_while_page;
create_page_navigation();
}
}
return page_numbers_html.join('');
}
function refresh_items_per_page_list()
{
var items_per_page_html = '';
$.each(settings.items_per_page_content, function(k, v){
k = (typeof k === 'Number') ? simple_number_formatter(k, 0, settings.thousands_separator) : k;
v = parseInt(v);
items_per_page_html += '<option value="' + v + '"';
items_per_page_html += v === items_per_page ? ' selected' : '';
items_per_page_html += '>' + k + '</option>\n';
});
return items_per_page_html;
}
function refresh_specific_page_list(page_number)
{
var select_html = '';
for(var i=1; i<=page_count; i++)
{
select_html += '<option value="' + i + '"';
select_html += i === page_number ? ' selected' : '';
select_html += '>' + simple_number_formatter(i, 0, settings.thousands_separator) + '</option>\n';
}
return select_html;
}
function refresh_simple_pagination(page_number)
{
var item_range_min = page_number * items_per_page - items_per_page,
item_range_max = item_range_min + items_per_page;
item_range_max = item_range_max > item_count ? item_count : item_range_max;
refresh_page(page_number, item_range_min, item_range_max);
if(settings.use_first)
{
$(container_id + ' .' + settings.html_prefix + '-first').html(refresh_first(page_number));
}
if(settings.use_previous)
{
$(container_id + ' .' + settings.html_prefix + '-previous').html(refresh_previous(page_number));
}
if(settings.use_next)
{
$(container_id + ' .' + settings.html_prefix + '-next').html(refresh_next(page_number));
}
if(settings.use_last)
{
$(container_id + ' .' + settings.html_prefix + '-last').html(refresh_last(page_number));
}
if(settings.use_page_numbers && number_of_visible_page_numbers !== 0)
{
$(container_id + ' .' + settings.html_prefix + '-page-numbers').html(refresh_page_numbers(page_number));
}
if(settings.use_page_x_of_x)
{
var page_x_of_x_html = '' + settings.page_x_of_x_content + ' ' + simple_number_formatter(page_number, 0, settings.thousands_separator) + ' of ' + simple_number_formatter(page_count, 0, settings.thousands_separator);
$(container_id + ' .' + settings.html_prefix + '-page-x-of-x').html(page_x_of_x_html);
}
if(settings.use_page_count)
{
$(container_id + ' .' + settings.html_prefix + '-page-count').html(page_count);
}
if(settings.use_showing_x_of_x)
{
var showing_x_of_x_html = settings.showing_x_of_x_content + ' ' + simple_number_formatter(item_range_min + 1, 0, settings.thousands_separator) + '-' + simple_number_formatter(item_range_max, 0, settings.thousands_separator) + ' of ' + simple_number_formatter(item_count, 0, settings.thousands_separator);
$(container_id + ' .' + settings.html_prefix + '-showing-x-of-x').html(showing_x_of_x_html);
}
if(settings.use_item_count)
{
$(container_id + ' .' + settings.html_prefix + '-item-count').html(item_count);
}
if(settings.use_items_per_page)
{
$(container_id + ' .' + settings.html_prefix + '-items-per-page').html(refresh_items_per_page_list);
}
if(settings.use_specific_page_list)
{
$(container_id + ' .' + settings.html_prefix + '-select-specific-page').html(refresh_specific_page_list(page_number));
}
}
refresh_simple_pagination(1);
$(container_id).on('click', settings.navigation_element + '[data-' + settings.html_prefix + '-page-number]', function(e)
{
e.preventDefault();
var page_number = +$(this).attr('data-' + settings.html_prefix + '-page-number');
refresh_simple_pagination(page_number);
});
$(container_id + ' .' + settings.html_prefix + '-items-per-page').change(function()
{
items_per_page = +$(this).val();
page_count = Math.ceil(item_count / items_per_page);
refresh_simple_pagination(1);
});
$(container_id + ' .' + settings.html_prefix + '-select-specific-page').change(function()
{
specific_page = +$(this).val();
refresh_simple_pagination(specific_page);
});
});
};
$.fn.simplePagination.defaults = {
pagination_container: 'tbody',
html_prefix: 'simple-pagination',
navigation_element: 'a',//button, span, div, et cetera
items_per_page: 25,
number_of_visible_page_numbers: 5,
//
use_page_numbers: true,
use_first: true,
use_previous: true,
use_next: true,
use_last: true,
//
use_page_x_of_x: true,
use_page_count: false,// Can be used to combine page_x_of_x and specific_page_list
use_showing_x_of_x: true,
use_item_count: false,
use_items_per_page: true,
use_specific_page_list: true,
//
first_content: 'First', //e.g. '<<'
previous_content: 'Previous', //e.g. '<'
next_content: 'Next', //e.g. '>'
last_content: 'Last', //e.g. '>>'
page_x_of_x_content: 'Page',
showing_x_of_x_content: 'Showing',
//
items_per_page_content: {
'Five': 5,
'Ten': 10,
'Twenty-five': 25,
'Fifty': 50,
'One hundred': 100
},
thousands_separator: ','
};
})(jQuery);