generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathBook Library.html
162 lines (145 loc) · 6.19 KB
/
Book Library.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
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html>
<head>
<title>Book Library</title>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body class="container mt-4">
<h2>Book Library</h2>
<!-- Form to add a new book -->
<div class="mb-4">
<div class="form-row">
<div class="form-group col-md-3">
<input type="text" id="title" class="form-control" placeholder="Title" />
</div>
<div class="form-group col-md-3">
<input type="text" id="author" class="form-control" placeholder="Author" />
</div>
<div class="form-group col-md-2">
<input type="number" id="pages" class="form-control" placeholder="Pages" />
</div>
<div class="form-group col-md-2 d-flex align-items-center">
<input type="checkbox" id="check" class="mr-2" />
<label for="check" class="mb-0">Read?</label>
</div>
<div class="form-group col-md-2">
<button onclick="submit()" class="btn btn-primary btn-block">Add</button>
</div>
</div>
</div>
<!-- Book table -->
<table id="display" class="table table-bordered">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Author</th>
<th>Pages</th>
<th>Read</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Rows will be dynamically added here -->
</tbody>
</table>
<!-- Script Section (unchanged JS from your code) -->
<script>
let myLibrary = [];
window.addEventListener("load", function () {
populateStorage();
render();
});
function populateStorage() {
if (myLibrary.length === 0) {
let bookList = [
new Book("Robison Crusoe", "Daniel Defoe", "252", true),
new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true),
new Book("The Great Gatsby", "F. Scott Fitzgerald", "180", true),
new Book("The Catcher in the Rye", "J.D Salinger", "277", true),
new Book("The Alchemist", "Paulo Coelho", "208", true),
new Book("Harry Potter", "J.K Rowling", "309", true),
new Book("The Lord of the Rings", "J.R.R Tolkien", "1178", true),
new Book("The Hobbit", "J.R.R Tolkien", "310", true),
new Book("The Three Musketeers", "Alexandre Dumas", "700", true),
new Book("The Little Prince", "Antoine de Saint-Exupery", "96", true),
new Book("The Psychology of Money", "Morgan Housel", "256", true),
new Book("How to Win Friends", "Dale Carnegie", "288", true),
new Book("Civilization", "Niall Ferguson", "512", true),
new Book("Around the World in 80 Days", "Jules Verne", "240", true),
new Book("Animal Farm", "George Orwell", "112", true),
new Book("Overthinking", "Ryan Creed", "256", true),
new Book("Get on with Anyone", "Katherine Stothart", "256", true),
new Book("Critical Thinking", "Jerrell Foreman", "256", true),
new Book("Emotional Intelligence", "Hanso", "256", true),
new Book("Fahrenheit 451", "Ray Bradbury", "256", true)
];
myLibrary.push(...bookList);
}
}
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
}
function submit() {
const titleInput = document.getElementById("title").value;
const authorInput = document.getElementById("author").value;
const pagesInput = document.getElementById("pages").value;
const isRead = document.getElementById("check").checked;
if (!titleInput || !authorInput || !pagesInput) {
alert("Please fill all fields!");
return;
}
const newBook = new Book(titleInput, authorInput, pagesInput, isRead);
myLibrary.push(newBook);
// Clear form inputs
document.getElementById("title").value = "";
document.getElementById("author").value = "";
document.getElementById("pages").value = "";
document.getElementById("check").checked = false;
render();
}
function render() {
const table = document.getElementById("display").getElementsByTagName("tbody")[0];
table.innerHTML = ""; // Clear table body
myLibrary.forEach((book, index) => {
let row = table.insertRow();
row.insertCell(0).textContent = book.title;
row.insertCell(1).textContent = book.author;
row.insertCell(2).textContent = book.pages;
// Read toggle button
let readCell = row.insertCell(3);
let readBtn = document.createElement("button");
readBtn.className = "btn btn-sm " + (book.check ? "btn-success" : "btn-secondary");
readBtn.textContent = book.check ? "Yes" : "No";
readBtn.onclick = () => {
myLibrary[index].check = !myLibrary[index].check;
render();
};
readCell.appendChild(readBtn);
// Delete button
let deleteCell = row.insertCell(4);
let delBtn = document.createElement("button");
delBtn.className = "btn btn-sm btn-danger";
delBtn.textContent = "Delete";
delBtn.onclick = () => {
if (confirm(`Delete "${book.title}"?`)) {
myLibrary.splice(index, 1);
render();
}
};
deleteCell.appendChild(delBtn);
});
}
</script>
</body>
</html>