-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (113 loc) · 4.7 KB
/
script.js
File metadata and controls
148 lines (113 loc) · 4.7 KB
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
//Global Variables
const studentList = document.querySelector('.student-list');
/* ============================================ */
/* showPage Function */
/* ============================================ */
//This function will create and insert/append the elements needed to display a "page" of nine students
function showPage (list, page) {
const firstStudent = (page * 9) - 9;
const lastStudent = page * 9;
studentList.innerHTML = '';
for (let i = 0; i < list.length; i++) {
if (i >= firstStudent && i < lastStudent) {
const studentItem =
`<li class="student-item cf">
<div class="student-details">
<img class="avatar" src="${data[i].picture.large}" alt="Profile Picture">
<h3>${data[i].name.first + ' ' + data[i].name.last}</h3>
<span class="email"> ${data[i].email}</span>
</div>
<div class="joined-details">
<span class="date">${data[i].registered.date}</span>
</div>
</li>`
;
studentList.insertAdjacentHTML("beforeend", studentItem);
}; //if statement
}; //for Loop
}; //function showPage
/* ============================================ */
/* addPagination Function */
/* ============================================ */
//This function will create and insert/append the elements needed for the pagination buttons
function addPagination (list) {
const pageCalc = Math.ceil(list.length / 9); //Output is 5 pages unless more data is added.
const linkList = document.querySelector('.link-list');
linkList.innerHTML = '';
for (let i = 1; i < list.length; i++) {
if (i <= pageCalc) {
const pageNumber =
`<li>
<button type="button">${[i]}</button>
</li>
`;
linkList.insertAdjacentHTML("beforeend", pageNumber);
};//if statement
};//for Loop
const pageButton = document.querySelector('button');
pageButton.className = 'active';
linkList.addEventListener('click', (e) => {
if (e.tagName = 'button') {
const pageButton = document.querySelector('.active');
pageButton.className = '';
e.target.className = 'active';
showPage(list, e.target.textContent);
}//if statement
});//addEventListener
};//function addPagination
showPage(data, 1);
addPagination(data);
/* ============================================ */
/* nameSearch Function */
/* ============================================ */
// This function will search for students based on the search input.
//Dynamically inserts the Heading Elements <h2> and search bar
const pageHeader = document.querySelector('.header');
pageHeader.innerHTML = '';
const studentNav = `
<h2>Students</h2>
<label for="search" class="student-search">
<span>Search by name</span>
<input type="search" oninput="nameSearch()" id="search" placeholder="Search by name...">
<button type="button"><img src="img/icn-search.svg" alt="Search icon"></button>
</label>
`;
pageHeader.insertAdjacentHTML("beforeend", studentNav);
function nameSearch(searchInput, names) {
// Filter so only student DATA that includes names that match the search value are output.
//create a new student list based on the earch matches. Use the new list as an argument for the showPage function.
//Add Pagination - use the new list as an argument for the addPagination function.
// Clicking on a pagination button will show the correct page/number of students.
const input = document.getElementById('search');
const submit = document.querySelector('button');
input.innerHTML = '';
console.log(searchInput);
console.log(names);
console.log(input);
console.log(submit);
for (let i = 1; i < names.length; i++) {
if (names[i].name.first.includes(searchInput) || names[i].name.last.includes(searchInput)) {
showPage(names, 1);
// } else {
// const noMatch =
// `<h3>No Results Found</h3>`;
// studentList.insertAdjacentHTML("beforeend", noMatch);
// }
}
}
/* ============================================ */
/* Event Listeners */
/* ============================================ */
// Submit button click event listener
submit.addEventListener('click', (event) => {
event.preventDefault();
nameSearch(search, data);
console.log('Submit button is functional!');
});
// Submit Keyup Event Listener
input.addEventListener('keyup', () => {
nameSearch(search, data);
console.log('Keyup event on the Search input is functional!');
});
}
nameSearch(search, data);