forked from denhartog/jquery-simple-pagination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
95 lines (82 loc) · 3.44 KB
/
js.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
(function($){
var items = $('#first-container').children(),
item_count = items.length,
items_per_page = 5,
page_count = Math.ceil(item_count / items_per_page),
show_first = true,
show_previous = true,
show_next = true,
show_last = true;
function refresh_page_navigation(page_number){
var page_numbers_html = '',
first_html = '',
previous_html = '',
next_html = '',
last_html = '';
//if there is more than ONE page of results then show the page-to-page navigation
if(page_count > 1){
//if we are on a page that is NOT the first, show first/previous navigation
if(page_number > 1){
//do we want to see the 'first' navigation?
if(show_first){
first_html += '<a href="#" class="simplePagination-navigation-first" data-simplePagination-page-number="' + 1 + '">First</a>';
}
//do we want to see the 'previous' navigation?
if(show_previous){
previous_html += '<a href="#" class="simplePagination-navigation-previous" data-simplePagination-page-number="' + (page_number - 1) + '">Previous</a>';
}
}
//if we are on a page that is NOT the last, show next/last navigation
if(page_number < page_count){
//do we want to see the 'next' navigation?
if(show_next){
next_html += '<a href="#" class="simplePagination-navigation-next" data-simplePagination-page-number="' + (page_number + 1) + '">Next</a>';
}
//do we want to see the 'last' navigation?
if(show_last){
last_html += '<a href="#" class="simplePagination-navigation-last" data-simplePagination-page-number="' + page_count + '">Last</a>';
}
}
//create a navigational link for every page
var current_page = 0;
while(page_count > current_page){
current_page++;
page_numbers_html += '<a href="#" class="simplePagination-navigation-page';
if(page_number === current_page){
page_numbers_html += ' simplePagination-navigation-page-active';
}
page_numbers_html += '" data-simplePagination-page-number="' + current_page + '">' + current_page + '</a>';
}
}
$('.simplePagination-navigation').html(first_html + previous_html + page_numbers_html + next_html + last_html);
}
function refresh_current_page(page_number){
$('.simplePagination-current-page-text').text('Page ' + page_number + ' of ' + page_count);
}
function refresh_current_items(page_number, page_range_min, page_range_max){
$('.simplePagination-current-items-text').text('Showing ' + page_range_min + '-' + page_range_max + ' of ' + item_count);
}
function show_page(page_number, page_range_min, page_range_max){
items.hide();
items.slice(page_range_min, page_range_max).show();
}
function refresh_page_information(page_number){
var page_range_min = page_number * items_per_page - items_per_page,
page_range_max = page_range_min + items_per_page;
show_page(page_number, page_range_min, page_range_max);
refresh_page_navigation(page_number);
refresh_current_page(page_number);
refresh_current_items(page_number, page_range_min + 1, page_range_max);
}
refresh_page_information(1);
$('.simplePagination-navigation').on('click', 'a', function(){
var page_number = $(this).attr('data-simplePagination-page-number');
refresh_page_information(+page_number);////force Number type with + OR parseInt()
return false;//==function(e){e.preventDefault();}
});
$('.simplePagination-update-items-per-page').change(function(){
items_per_page = +$(this).val();//force Number type with + OR parseInt()
page_count = Math.ceil(item_count / items_per_page);
refresh_page_information(1);
});
})(jQuery);