-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (56 loc) · 1.75 KB
/
app.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
const allStudents = $('.student-item');
let searchResults = [];
let lastButton;
// Create a function to hide all students
function hideAllStudents() {
allStudents.each(function () {
this.style.display = 'none';
});
}
//Create a function to generate the html that will hold the button element
function generateButton (number, list) {
return `<li><a>${number}</a></li>`;
}
// Create a function to display only the ten I want at any given time
function printPage(pageNumber, array) {
hideAllStudents();
let init = pageNumber * 10;
let start = init - 10;
// If user clicks last button, present only the remaining students
if (pageNumber == lastButton) {
let remainder = (array.length % 10) + start;
for (let i = start; i < remainder; i++) {
searchResults[i].style.display = '';
}
// Print 10 students, as long as the last button is not clicked
} else {
// Show 10 students
for (let i = start; i < init; i++) {
searchResults[i].style.display = '';
}
}
}
// Create a function to add the correct amount of functioning buttons to the page
function paginate(array) {
let numberOfPages = Math.ceil((array.length)/10);
let paginationHTML = '<div class="pagination"><ul id="buttons"></ul></div>';
let html = '';
searchResults = array;
// Generate all page buttons
for (let i=0; i < numberOfPages; i++) {
html += generateButton((i+1));
}
// Add all html to index.html
$('.page').append(paginationHTML);
$('#buttons').append(html);
lastButton = $('#buttons li').length;
// Print page when button is clicked
$('li').on('click', () => {
let num = $(event.target).text();
printPage(num, searchResults);
});
}
// Initialize page by showing first 10 students
searchResults = allStudents;
printPage(1, searchResults);
paginate(searchResults);