-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
156 lines (129 loc) · 4.63 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<form id="book-form">
<div>
<input id ='Image'type="file" />
</div>
<div>
<label for="author">Author</label>
<input type="text" id="author">
</div>
<div>
<label for="isbn">ISBN#</label>
<input type="text" id="isbn">
</div>
<input type="submit" value="Add Book">
</form>
<table>
<thead>
<tr>
<th>Author</th>
<th>ISBN#</th>
<th></th>
</tr>
</thead>
<tbody id="book-list"></tbody>
</table>
</div>
<table style="display: none;" class="hiddenDiv">
<thead>
<tr>
<th class="htmlAuthor">Author</th>
<th class="htmlIsbn">ISBN#</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<img src="" id="StudentPhoto" style="width:100px;height:100px">
</td>
</tr>
</tbody>
</table>
<!-- -------------------------------------------------- -->
<script>
let ArrayofObject = [];
let trs = document.querySelector("#book-list").getElementsByTagName('tr');
function GetFromLocalStorage() {
if (localStorage.getItem('ArrayofObject') === null) {
ArrayofObject = [];
} else {
ArrayofObject = JSON.parse(localStorage.getItem('ArrayofObject'));
ArrayofObject.forEach(element => {
addtoUI(element);
});
}
}
GetFromLocalStorage();
let ImageURL = "";
document.querySelector('#book-form').addEventListener('submit', (e) => {
e.preventDefault();
const author = document.querySelector('#author').value;
const isbn = document.querySelector('#isbn').value;
let newObject = {
Author: author,
Isbn: isbn,
image: ImageBlobData
}
ArrayofObject.push(newObject);
addtoUI(newObject);
localStorage.setItem('ArrayofObject', JSON.stringify(ArrayofObject));
});
var fileTag = document.getElementById("Image");
fileTag.addEventListener("change", function() {
changeImage(this);
});
function changeImage(input) {
var reader;
if (input.files && input.files[0]) {
reader = new FileReader();
reader.onload = function(e) {
ImageBlobData = e.target.result;
}
reader.readAsDataURL(input.files[0]);
}
}
function addtoUI(object) {
const list = document.querySelector('#book-list');
const row = document.createElement('tr');
row.innerHTML = `
<td>${object.Author}</td>
<td>${object.Isbn}</td>
<td><button class="btn btn-danger btn-sm delete">X</button></td>
<td><button class="view">View</button></td>
`;
list.appendChild(row);
}
function removeFromUIAndViewToUI(el) {
let Testarray = [...trs];
if (el.classList.contains('delete')) {
el.parentElement.parentElement.remove();
ArrayofObject.splice(Testarray.indexOf(el.parentElement.parentElement), 1);
document.querySelector('.hiddenDiv').style = "display:none";
document.querySelector('.htmlAuthor').innerHTML = "";
document.querySelector('.htmlIsbn').innerHTML = "";
document.querySelector('#StudentPhoto').src = "";
localStorage.setItem('ArrayofObject', JSON.stringify(ArrayofObject));
}
if (el.classList.contains('view')) {
let temporary = ArrayofObject[Testarray.indexOf(el.parentElement.parentElement)];
document.querySelector('.hiddenDiv').style = "display:block";
document.querySelector('#StudentPhoto').src = temporary.image;
document.querySelector('.htmlAuthor').innerHTML = temporary.Author;
document.querySelector('.htmlIsbn').innerHTML = temporary.Isbn;
}
}
document.querySelector('#book-list').addEventListener('click', (e) => {
removeFromUIAndViewToUI(e.target);
});
</script>
</body>
</html>