-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagination.html
153 lines (135 loc) · 4.59 KB
/
Pagination.html
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
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagination with Dynamic Content Loading</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
max-width: 800px;
margin: 0 auto;
}
.post {
background-color: #f4f4f4;
padding: 15px;
margin-bottom: 15px;
border-radius: 5px;
}
.post h2 {
margin-top: 0;
}
.pagination-container {
display: flex;
justify-content: center;
align-items: center;
margin-top: 20px;
}
.pagination-button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}
.pagination-button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
#pageInfo {
margin: 0 10px;
}
#loading {
text-align: center;
display: none;
}
</style>
</head>
<body>
<h1>Blog Posts</h1>
<div id="postsContainer"></div>
<div id="loading">Loading...</div>
<div class="pagination-container">
<button id="prevButton" class="pagination-button" disabled>Previous</button>
<span id="pageInfo"></span>
<button id="nextButton" class="pagination-button">Next</button>
</div>
<script>
const postsContainer = document.getElementById('postsContainer');
const prevButton = document.getElementById('prevButton');
const nextButton = document.getElementById('nextButton');
const pageInfo = document.getElementById('pageInfo');
const loading = document.getElementById('loading');
let currentPage = 1;
const postsPerPage = 5;
let totalPosts = 0;
async function fetchPosts(page) {
loading.style.display = 'block';
// In a real application, you would fetch data from a server
// Here, we're simulating an API call with a delay
await new Promise(resolve => setTimeout(resolve, 500));
// Simulated data (in reality, this would come from your server)
totalPosts = 23; // Total number of posts (for this example)
const startIndex = (page - 1) * postsPerPage;
const endIndex = startIndex + postsPerPage;
const posts = [];
for (let i = startIndex + 1; i <= Math.min(endIndex, totalPosts); i++) {
posts.push({
id: i,
title: `Post ${i}`,
excerpt: `This is a summary of post ${i}. Click to read more...`
});
}
loading.style.display = 'none';
return posts;
}
function displayPosts(posts) {
postsContainer.innerHTML = '';
posts.forEach(post => {
const postElement = document.createElement('div');
postElement.classList.add('post');
postElement.innerHTML = `
<h2>${post.title}</h2>
<p>${post.excerpt}</p>
`;
postsContainer.appendChild(postElement);
});
}
function updatePagination() {
const totalPages = Math.ceil(totalPosts / postsPerPage);
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === totalPages;
}
async function loadPage(page) {
const posts = await fetchPosts(page);
displayPosts(posts);
updatePagination();
}
prevButton.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
loadPage(currentPage);
}
});
nextButton.addEventListener('click', () => {
const totalPages = Math.ceil(totalPosts / postsPerPage);
if (currentPage < totalPages) {
currentPage++;
loadPage(currentPage);
}
});
// Initial load
loadPage(currentPage);
</script>
</body>
</html>