Skip to content

Commit e7c2242

Browse files
authored
Merge pull request #479 from naikmubashir/gh-pages
Add pagination
2 parents b450e0d + fadf6d9 commit e7c2242

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
rel="stylesheet"
1212
/>
1313
<link rel="stylesheet" href="styles/styles.css" />
14+
<link rel="stylesheet" href="styles/pagination.css" />
1415
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
1516
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
1617
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-database.js"></script>
@@ -132,6 +133,14 @@ <h1 class="main-heading">Awesome GitHub Profile READMEs</h1>
132133
<div class="profiles">
133134
<!-- Profiles will be dynamically loaded here -->
134135
</div>
136+
137+
<!-- Buttons for pagination -->
138+
<div class="pagination">
139+
<button id="prev-page" disabled>Previous</button>
140+
<span id="page-info"></span>
141+
<button id="next-page">Next</button>
142+
</div>
143+
135144
</div>
136145
<footer id="Contact" class="footer-2">
137146
<div class="footer-container">

scripts/retriveprofile.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ var firebaseConfig = {
1414
// Initialize Firebase
1515
firebase.initializeApp(firebaseConfig);
1616

17+
let currentPage=1;
18+
const profilesPerPage = 12; // Number of profiles per page
19+
1720
document.addEventListener("DOMContentLoaded", function () {
1821
let contributors = [];
1922
const noProfilesMessage = document.querySelector(".no-profiles-message");
2023
noProfilesMessage.style.display="none"
21-
function renderProfiles(filter = "") {
24+
function renderProfiles(filter = "", page=1, profilesPerPage=12) {
2225

2326
const container = document.querySelector(".profiles");
2427
container.innerHTML = "";
@@ -34,7 +37,15 @@ document.addEventListener("DOMContentLoaded", function () {
3437

3538
return
3639
}
37-
contributors.forEach((contributor,index) => {
40+
41+
// Calculate start and end index for pagination
42+
const startIndex = (page - 1) * profilesPerPage;
43+
const endIndex = startIndex + profilesPerPage;
44+
45+
// Slice the filtered contributors based on the current page
46+
const paginatedContributors = filteredContributors.slice(startIndex, endIndex);
47+
48+
paginatedContributors.forEach((contributor,index) => {
3849
noProfilesMessage.style.display = "none";
3950

4051
if (contributor.login.toLowerCase().includes(filter.toLowerCase())) {
@@ -175,14 +186,43 @@ document.addEventListener("DOMContentLoaded", function () {
175186

176187
}
177188
});
189+
190+
// Update pagination controls
191+
updatePaginationControls(page, profilesPerPage, filteredContributors.length);
192+
193+
194+
}
195+
196+
function updatePaginationControls(currentPage, profilesPerPage, totalProfiles) {
197+
const totalPages = Math.ceil(totalProfiles / profilesPerPage);
198+
const pageInfo = document.getElementById("page-info");
199+
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
200+
201+
// Disable previous button if on the first page
202+
document.getElementById("prev-page").disabled = currentPage === 1;
203+
204+
// Disable next button if on the last page
205+
document.getElementById("next-page").disabled = currentPage === totalPages;
178206
}
179207

208+
document.getElementById("prev-page").addEventListener("click", function () {
209+
if (currentPage > 1) {
210+
currentPage--;
211+
renderProfiles("", currentPage, profilesPerPage);
212+
}
213+
});
214+
215+
document.getElementById("next-page").addEventListener("click", function () {
216+
currentPage++;
217+
renderProfiles("", currentPage, profilesPerPage);
218+
});
219+
180220
// Fetch contributors data
181221
fetch("https://raw.githubusercontent.com/recodehive/awesome-github-profiles/main/.all-contributorsrc")
182222
.then((response) => response.json())
183223
.then((data) => {
184224
contributors = data.contributors;
185-
renderProfiles();
225+
renderProfiles("", 1, profilesPerPage); //Start on page one
186226
});
187227

188228
// Add event listener to the search bar

styles/pagination.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.pagination {
2+
display: flex;
3+
justify-content: center;
4+
align-items: center;
5+
margin-top: 20px;
6+
}
7+
8+
.pagination button {
9+
font-size: medium;
10+
margin: 0 10px;
11+
padding: 10px 20px;
12+
background-color: #007bff;
13+
color: white;
14+
border: none;
15+
border-radius: 5px;
16+
cursor: pointer;
17+
}
18+
19+
.pagination button:hover {
20+
background-color: #3a90d5; /* Background color on hover */
21+
transform: scale(1.05); /* Slightly enlarge button on hover */
22+
}
23+
24+
.pagination button:disabled {
25+
background-color: #c0c0c0;
26+
cursor: not-allowed;
27+
}
28+
29+
.pagination #page-info {
30+
font-weight: bold;
31+
}

0 commit comments

Comments
 (0)