-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathajax-store.html
More file actions
72 lines (59 loc) · 1.87 KB
/
ajax-store.html
File metadata and controls
72 lines (59 loc) · 1.87 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Online Store</title>
</head>
<body>
<h1>My Tool Store</h1>
<table id="products">
<thead>
<tr>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
<th>Categories</th>
</tr>
</thead>
<tbody id="insertProducts">
<tr>
<td>Hammer</td>
<td>25</td>
<td>tool</td>
<td>20</td>
</tr>
</tbody>
</table>
<button id="refresh-btn">Refresh</button>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
"use strict";
// TODO: Create an AJAX GET request for the file under data/inventory.json
function refreshFromJson() {
$.get('data/inventory.json').done(function(tools) {
$('#insertProducts').html('');
for (var i = 0; i < tools.length; i += 1) {
$('#insertProducts').append(buildToolHTML(tools[i]));
}
});
}
// TODO: Take the data from inventory.json and append it to the products table
// HINT: Your data should come back as a JSON object; use console.log() to inspect
// its contents and fields
// HINT: You will want to target #insertProducts for your new HTML elements
function buildToolHTML(tool) {
var html = '';
html += '<tr>\n' +
' <td>' + tool.title + '</td>\n' +
' <td>' + tool.quantity + '</td>\n' +
' <td>' + tool.categories.join(', ') + '</td>\n' +
' <td>' + tool.price + '</td>\n' +
' </tr>';
return html;
}
$('#refresh-btn').click(refreshFromJson);
refreshFromJson();
});
</script>
</body>
</html>