@@ -55,53 +55,43 @@ function Book(title, author, pages, check) {
5555}
5656
5757function render ( ) {
58- let table = document . getElementById ( "display" ) ;
59- let rowsNumber = table . rows . length ;
60- //delete old table
61- for ( let n = rowsNumber - 1 ; n > 0 ; n -- ) {
62- table . deleteRow ( n ) ;
63- }
64- //insert updated row and cells
65- let length = myLibrary . length ;
66- for ( let i = 0 ; i < length ; i ++ ) {
67- let row = table . insertRow ( 1 ) ;
68- let titleCell = row . insertCell ( 0 ) ;
69- let authorCell = row . insertCell ( 1 ) ;
70- let pagesCell = row . insertCell ( 2 ) ;
71- let wasReadCell = row . insertCell ( 3 ) ;
72- let deleteCell = row . insertCell ( 4 ) ;
58+ const table = document . getElementById ( "display" ) ;
59+ const tbody = table . querySelector ( "tbody" ) ;
60+
61+ tbody . innerHTML = "" ;
62+
63+ for ( let i = 0 ; i < myLibrary . length ; i ++ ) {
64+ const row = tbody . insertRow ( ) ;
65+ const titleCell = row . insertCell ( 0 ) ;
66+ const authorCell = row . insertCell ( 1 ) ;
67+ const pagesCell = row . insertCell ( 2 ) ;
68+ const wasReadCell = row . insertCell ( 3 ) ;
69+ const deleteCell = row . insertCell ( 4 ) ;
70+
7371 titleCell . textContent = myLibrary [ i ] . title ;
7472 authorCell . textContent = myLibrary [ i ] . author ;
7573 pagesCell . textContent = myLibrary [ i ] . pages ;
7674
77- //add and wait for action for read/unread button
78- let changeBut = document . createElement ( "button" ) ;
79- changeBut . id = i ;
80- changeBut . className = "btn btn-success" ;
81- wasReadCell . appendChild ( changeBut ) ;
82- let readStatus = "" ;
83- if ( myLibrary [ i ] . check == true ) {
84- readStatus = "Yes" ;
85- } else {
86- readStatus = "No" ;
87- }
88- changeBut . textContent = readStatus ;
75+ const changeBtn = document . createElement ( "button" ) ;
76+ changeBtn . className = "btn btn-success" ;
77+ changeBtn . textContent = myLibrary [ i ] . check ? "Yes" : "No" ;
78+ wasReadCell . appendChild ( changeBtn ) ;
8979
90- changeBut . addEventListener ( "click" , function ( ) {
80+ changeBtn . addEventListener ( "click" , function ( ) {
9181 myLibrary [ i ] . check = ! myLibrary [ i ] . check ;
9282 render ( ) ;
9383 } ) ;
9484
95- //add delete button to every row and render again
96- let delButton = document . createElement ( "button" ) ;
97- delButton . id = i + 5 ;
98- deleteCell . appendChild ( delButton ) ;
99- delButton . className = "btn btn-warning" ;
100- delButton . textContent = "Delete" ;
101- delButton . addEventListener ( "click" , function ( ) {
102- alert ( `You've deleted title: ${ myLibrary [ i ] . title } ` ) ;
85+ const deleteBtn = document . createElement ( "button" ) ;
86+ deleteBtn . className = "btn btn-warning" ;
87+ deleteBtn . textContent = "Delete" ;
88+ deleteCell . appendChild ( deleteBtn ) ;
89+
90+ deleteBtn . addEventListener ( "click" , function ( ) {
91+ const deletedTitle = myLibrary [ i ] . title ;
10392 myLibrary . splice ( i , 1 ) ;
10493 render ( ) ;
94+ alert ( `You've deleted title: ${ deletedTitle } ` ) ;
10595 } ) ;
10696 }
10797}
0 commit comments