Skip to content

Commit e18e196

Browse files
committed
pagination
1 parent f98e5db commit e18e196

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

Diff for: Pagination/index.css

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.nav-bar {
2+
display: flex;
3+
flex-wrap: wrap;
4+
align-items: center;
5+
justify-content: center;
6+
padding: 1rem;
7+
margin-bottom: 5px;
8+
gap: 1rem;
9+
border-bottom: solid 1px #aaa;
10+
background-color: #aaa;
11+
}
12+
13+
nav a {
14+
min-width: 9rem;
15+
border-radius: 0.3rem;
16+
border: solid black;
17+
background-color: rgb(111, 175, 231);
18+
text-align: center;
19+
text-decoration-line: none;
20+
padding: 5px;
21+
}
22+
23+
* {
24+
margin: 0;
25+
padding: 0;
26+
background-color: rgb(23, 30, 30);
27+
}
28+
29+
#container {
30+
display: flex;
31+
flex-wrap: wrap;
32+
justify-content: center;
33+
gap: 20px;
34+
}
35+
36+
#container div {
37+
text-align: center;
38+
}
39+
40+
#container img {
41+
height: 400px;
42+
width: auto;
43+
border-radius: 10px;
44+
}
45+
button {
46+
background-color: rgb(214, 183, 206);
47+
height: 30px;
48+
49+
width: 30px;
50+
align-items: center;
51+
}
52+
#btn {
53+
align-items: center;
54+
justify-content: center;
55+
margin: 5px;
56+
}

Diff for: Pagination/index.html

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Pagination</title>
7+
<link rel="stylesheet" href="index.css" />
8+
</head>
9+
<body>
10+
<nav class="nav-bar">
11+
<a href="/index.html">Home</a>
12+
<a target="_blank" href="https://youtu.be/2s2sZm5gy-o?feature=shared"
13+
>Youtube channel</a
14+
>
15+
<a
16+
target="_blank"
17+
href="https://github.com/RohitSharma50/Javascript_Machine_coding"
18+
>Github</a
19+
>
20+
</nav>
21+
<div id="container"></div>
22+
23+
<script src="index.js" defer></script>
24+
</body>
25+
</html>

Diff for: Pagination/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
document.addEventListener("DOMContentLoaded", function () {
2+
let list = [];
3+
let page = 1;
4+
async function fetchProduct() {
5+
try {
6+
const result = await fetch("https://dummyjson.com/products?limit=100");
7+
const data = await result.json();
8+
9+
if (data && data.products) {
10+
list = data.products;
11+
render(page);
12+
}
13+
} catch (error) {
14+
console.log(error, "Fetching Error occured");
15+
}
16+
}
17+
function render(page) {
18+
const mainDiv = document.getElementById("container");
19+
mainDiv.innerHTML = "";
20+
list.slice(page * 10 - 10, page * 10).forEach((prod) => {
21+
const element = document.createElement("div");
22+
element.id = "data";
23+
element.innerHTML = `<img src="${prod.images[0]}" alt="${prod.title}" style="height: 400px; width:auto" />`;
24+
mainDiv.appendChild(element);
25+
});
26+
createButton(page);
27+
}
28+
29+
function createButton(page) {
30+
const buttonContainer = document.createElement("div");
31+
const mainDiv = document.getElementById("container");
32+
33+
const backButton = document.createElement("button");
34+
backButton.innerHTML = "🔙";
35+
backButton.addEventListener("click", function () {
36+
render(page - 1);
37+
});
38+
if (page > 1) {
39+
buttonContainer.appendChild(backButton);
40+
mainDiv.appendChild(buttonContainer);
41+
}
42+
for (let i = 1; i < 10; i++) {
43+
const btn = document.createElement("button");
44+
btn.innerText = i;
45+
btn.id = "btn";
46+
btn.addEventListener("click", function () {
47+
render(i);
48+
});
49+
buttonContainer.appendChild(btn);
50+
mainDiv.appendChild(buttonContainer);
51+
}
52+
if (page < 10) {
53+
const nextButton = document.createElement("button");
54+
nextButton.innerHTML = "⏭";
55+
nextButton.addEventListener("click", function () {
56+
render(page + 1);
57+
});
58+
buttonContainer.appendChild(nextButton);
59+
mainDiv.appendChild(buttonContainer);
60+
}
61+
}
62+
63+
fetchProduct();
64+
});

Diff for: index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ <h1>Javascript-Challanges</h1>
4848
>Project 8 - Tic-Tac- Toe 📚
4949
</a>
5050
<a class="project-link" href="./Infinite Scroll/index.html"
51-
>Project 9 - Infinite Scroll📚
51+
>Project 9 - Infinite Scroll🫥
5252
</a>
5353
<a class="project-link" href="./Image Slider/index.html"
54-
>Project 10 - Image Slider 🫥
54+
>Project 10 - Image Slider 📚
5555
</a>
5656
<a class="project-link" href="./Holy Grail/index.html"
5757
>Project 10 - Holy Grail 🫥
5858
</a>
59+
<a class="project-link" href="./Pagination/index.html"
60+
>Project 11 - Pagination 👻
61+
</a>
5962
</div>
6063
</main>
6164
</body>

0 commit comments

Comments
 (0)