Skip to content

Commit 4f368c0

Browse files
committed
Updated pagination controls for blog.
1 parent 5b6f770 commit 4f368c0

4 files changed

Lines changed: 107 additions & 10 deletions

File tree

blog.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@
4242

4343
<section id="posts-list"></section>
4444
<div id="pagination">
45-
<button id="prev-button">Previous</button>
46-
<button id="next-button">Next</button>
45+
<label for="per-page-select" class="pagination-label">Per page:</label>
46+
<select id="per-page-select" aria-label="Posts per page">
47+
<option value="10" selected>10</option>
48+
<option value="20">20</option>
49+
<option value="50">50</option>
50+
</select>
51+
<button id="prev-button" aria-label="Previous page">Previous</button>
52+
<div id="page-jump">
53+
<label for="page-input" class="pagination-label">Page</label>
54+
<input id="page-input" type="number" min="1" value="1" aria-label="Current page" />
55+
<span id="total-pages" aria-live="polite"></span>
56+
</div>
57+
<button id="next-button" aria-label="Next page">Next</button>
4758
</div>
4859

4960
<a href="index.html" class="back-link">Back to Home</a>

css/blog.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,45 @@
209209
margin-top: 2rem;
210210
}
211211

212+
/* New pagination controls */
213+
#per-page-select, #page-input {
214+
font-family: inherit;
215+
font-size: 0.9rem;
216+
padding: 0.4rem 0.6rem;
217+
border: 1px solid #ccc;
218+
border-radius: 8px;
219+
background: #fff;
220+
color: #222;
221+
width: auto;
222+
}
223+
224+
#page-input {
225+
width: 5.2ch;
226+
text-align: left;
227+
}
228+
229+
.dark-mode #per-page-select, .dark-mode #page-input {
230+
background: #222;
231+
color: #eee;
232+
border-color: #444;
233+
}
234+
235+
#page-jump {
236+
display: flex;
237+
align-items: flex-end;
238+
gap: 0.3rem;
239+
}
240+
241+
.pagination-label {
242+
font-size: 0.8rem;
243+
opacity: 0.7;
244+
}
245+
246+
#total-pages {
247+
font-size: 0.9rem;
248+
min-width: 3ch;
249+
}
250+
212251
/* Match the theme-toggle style for pagination buttons */
213252
#prev-button,
214253
#next-button {

posts/js/blog.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/* blog.js - list of posts with tag filter, pagination, and search */
22

3-
// Constants
4-
const PER_PAGE = 10;
3+
// Pagination size (mutable via dropdown)
4+
let perPage = 10;
55

66
// DOM Elements
77
const list = document.getElementById("posts-list");
88
const btnBox = document.getElementById("tag-buttons");
99
const prevBtn = document.getElementById("prev-button");
1010
const nextBtn = document.getElementById("next-button");
11+
const perPageSelect = document.getElementById("per-page-select");
12+
const pageInput = document.getElementById("page-input");
13+
const totalPagesSpan = document.getElementById("total-pages");
1114
const searchBox = document.getElementById("search-box");
1215
const searchButton = document.getElementById("search-button");
1316

@@ -64,11 +67,24 @@ function renderPosts(posts, filterTag = null) {
6467
}
6568

6669
// Render the current page of posts
70+
function totalPages() {
71+
return Math.max(1, Math.ceil(filtered.length / perPage));
72+
}
73+
74+
function clampPage() {
75+
const tp = totalPages();
76+
if (currentPage > tp) currentPage = tp;
77+
if (currentPage < 1) currentPage = 1;
78+
}
79+
6780
function renderPage() {
68-
const start = (currentPage - 1) * PER_PAGE;
69-
renderPosts(filtered.slice(start, start + PER_PAGE));
81+
clampPage();
82+
const start = (currentPage - 1) * perPage;
83+
renderPosts(filtered.slice(start, start + perPage));
7084
prevBtn.disabled = currentPage === 1;
71-
nextBtn.disabled = start + PER_PAGE >= filtered.length;
85+
nextBtn.disabled = currentPage >= totalPages();
86+
if (pageInput) pageInput.value = currentPage;
87+
if (totalPagesSpan) totalPagesSpan.textContent = ` / ${totalPages()}`;
7288
}
7389

7490
/* -------------------- Event Handlers -------------------- */
@@ -83,12 +99,43 @@ prevBtn.addEventListener("click", () => {
8399

84100
// Handle pagination (next button)
85101
nextBtn.addEventListener("click", () => {
86-
if (currentPage * PER_PAGE < filtered.length) {
102+
if (currentPage < totalPages()) {
87103
currentPage++;
88104
renderPage();
89105
}
90106
});
91107

108+
// Change per-page size
109+
if (perPageSelect) {
110+
perPageSelect.addEventListener("change", () => {
111+
const val = parseInt(perPageSelect.value, 10);
112+
if ([10, 20, 50].includes(val)) {
113+
perPage = val;
114+
currentPage = 1;
115+
renderPage();
116+
}
117+
});
118+
}
119+
120+
// Page jump input
121+
if (pageInput) {
122+
pageInput.addEventListener("change", () => {
123+
let val = parseInt(pageInput.value, 10);
124+
if (Number.isNaN(val)) val = 1;
125+
currentPage = val;
126+
renderPage();
127+
});
128+
pageInput.addEventListener("keydown", e => {
129+
if (e.key === "Enter") {
130+
e.preventDefault();
131+
let val = parseInt(pageInput.value, 10);
132+
if (Number.isNaN(val)) val = currentPage;
133+
currentPage = val;
134+
renderPage();
135+
}
136+
});
137+
}
138+
92139
// Execute a search over titles + loaded body excerpts
93140
function executeSearch() {
94141
const query = searchBox.value.trim().toLowerCase();
@@ -176,7 +223,7 @@ fetch("./posts/metadata/entries.json")
176223
.then(data => {
177224
posts = data;
178225
filtered = posts;
179-
// Reset search and filter
226+
// Reset search and filter (also updates pagination UI)
180227
resetSearchAndFilter();
181228

182229
// Build tag buttons

posts/metadata/entries.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@
122122
"author": "R",
123123
"location": "Bobst 8F, New York, NY",
124124
"tags": ["math", "life", "notes"],
125-
"slug": "022-suranyi"
125+
"slug": "022-Suranyi"
126126
}
127127
]

0 commit comments

Comments
 (0)