@@ -12,9 +12,10 @@ const tableBody = document.getElementById("tableBody");
1212window . addEventListener ( "load" , ( ) => {
1313 populateStorage ( ) ;
1414 render ( ) ;
15+ submitBtn . addEventListener ( "click" , submitBook ) ;
1516} ) ;
1617
17- submitBtn . addEventListener ( "click" , submitBook ) ;
18+
1819
1920function Book ( title , author , pages , check ) {
2021 this . title = title ;
@@ -56,168 +57,49 @@ function submitBook() {
5657 render ( ) ;
5758}
5859
60+
5961function render ( ) {
60- // Clear the container in one operation
6162 tableBody . innerHTML = "" ;
6263
6364 myLibrary . forEach ( ( book , index ) => {
65+ // You can also use row = tableBody.insertRow() here!
6466 const row = document . createElement ( "tr" ) ;
6567
66- // Title Cell
67- const titleCell = document . createElement ( "td" ) ;
68+ // Title Cell (Index 0)
69+ const titleCell = row . insertCell ( 0 ) ;
6870 titleCell . textContent = book . title ;
69- row . appendChild ( titleCell ) ;
7071
71- // Author Cell
72- const authorCell = document . createElement ( "td" ) ;
72+ // Author Cell (Index 1)
73+ const authorCell = row . insertCell ( 1 ) ;
7374 authorCell . textContent = book . author ;
74- row . appendChild ( authorCell ) ;
7575
76- // Pages Cell
77- const pagesCell = document . createElement ( "td" ) ;
76+ // Pages Cell (Index 2)
77+ const pagesCell = row . insertCell ( 2 ) ;
7878 pagesCell . textContent = book . pages ;
79- row . appendChild ( pagesCell ) ;
8079
81- // Read Status Cell
82- const statusCell = document . createElement ( "td" ) ;
80+ // Read Status Cell (Index 3)
81+ const statusCell = row . insertCell ( 3 ) ;
8382 const statusBtn = document . createElement ( "button" ) ;
8483 statusBtn . className = "btn btn-sm btn-success" ;
85- // Ternary operator for simplification
8684 statusBtn . textContent = book . check ? "Yes" : "No" ;
87-
8885 statusBtn . addEventListener ( "click" , ( ) => {
8986 book . check = ! book . check ;
9087 render ( ) ;
9188 } ) ;
9289 statusCell . appendChild ( statusBtn ) ;
93- row . appendChild ( statusCell ) ;
9490
95- // Delete Cell
96- const deleteCell = document . createElement ( "td" ) ;
91+ // Delete Cell (Index 4)
92+ const deleteCell = row . insertCell ( 4 ) ;
9793 const deleteBtn = document . createElement ( "button" ) ;
9894 deleteBtn . className = "btn btn-sm btn-warning" ;
9995 deleteBtn . textContent = "Delete" ;
100-
10196 deleteBtn . addEventListener ( "click" , ( ) => {
102- const deletedTitle = book . title ;
10397 myLibrary . splice ( index , 1 ) ;
10498 render ( ) ;
105- // Alert after deletion logic is complete
106- alert ( `Deleted: ${ deletedTitle } ` ) ;
99+ alert ( `Deleted: ${ book . title } ` ) ;
107100 } ) ;
108101 deleteCell . appendChild ( deleteBtn ) ;
109- row . appendChild ( deleteCell ) ;
110102
111103 tableBody . appendChild ( row ) ;
112104 } ) ;
113105}
114-
115-
116-
117- /*
118-
119- let myLibrary = [];
120-
121- window.addEventListener("load", function (e) {
122- populateStorage();
123- render();
124- });
125-
126- function populateStorage() {
127- if (myLibrary.length == 0) {
128- let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
129- let book2 = new Book(
130- "The Old Man and the Sea",
131- "Ernest Hemingway",
132- "127",
133- true
134- );
135- myLibrary.push(book1);
136- myLibrary.push(book2);
137- render();
138- }
139- }
140-
141- const title = document.getElementById("title");
142- const author = document.getElementById("author");
143- const pages = document.getElementById("pages");
144- const check = document.getElementById("check");
145-
146- //check the right input from forms and if its ok -> add the new book (object in array)
147- //via Book function and start render function
148- function submit() {
149- if (
150- title.value == null ||
151- title.value == "" ||
152- pages.value == null ||
153- pages.value == ""
154- ) {
155- alert("Please fill all fields!");
156- return false;
157- } else {
158- let book = new Book(title.value, title.value, pages.value, check.checked);
159- library.push(book);
160- render();
161- }
162- }
163-
164- function Book(title, author, pages, check) {
165- this.title = title;
166- this.author = author;
167- this.pages = pages;
168- this.check = check;
169- }
170-
171- function render() {
172- let table = document.getElementById("display");
173- let rowsNumber = table.rows.length;
174- //delete old table
175- for (let n = rowsNumber - 1; n > 0; n-- {
176- table.deleteRow(n);
177- }
178- //insert updated row and cells
179- let length = myLibrary.length;
180- for (let i = 0; i < length; i++) {
181- let row = table.insertRow(1);
182- let titleCell = row.insertCell(0);
183- let authorCell = row.insertCell(1);
184- let pagesCell = row.insertCell(2);
185- let wasReadCell = row.insertCell(3);
186- let deleteCell = row.insertCell(4);
187- titleCell.innerHTML = myLibrary[i].title;
188- authorCell.innerHTML = myLibrary[i].author;
189- pagesCell.innerHTML = myLibrary[i].pages;
190-
191- //add and wait for action for read/unread button
192- let changeBut = document.createElement("button");
193- changeBut.id = i;
194- changeBut.className = "btn btn-success";
195- wasReadCell.appendChild(changeBut);
196- let readStatus = "";
197- if (myLibrary[i].check == false) {
198- readStatus = "Yes";
199- } else {
200- readStatus = "No";
201- }
202- changeBut.innerText = readStatus;
203-
204- changeBut.addEventListener("click", function () {
205- myLibrary[i].check = !myLibrary[i].check;
206- render();
207- });
208-
209- //add delete button to every row and render again
210- let delButton = document.createElement("button");
211- delBut.id = i + 5;
212- deleteCell.appendChild(delBut);
213- delBut.className = "btn btn-warning";
214- delBut.innerHTML = "Delete";
215- delBut.addEventListener("clicks", function () {
216- alert(`You've deleted title: ${myLibrary[i].title}`);
217- myLibrary.splice(i, 1);
218- render();
219- });
220- }
221- }
222-
223- */
0 commit comments