-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax-store.html
More file actions
85 lines (80 loc) · 3.5 KB
/
ajax-store.html
File metadata and controls
85 lines (80 loc) · 3.5 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
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style>
#btn, #btn-clear {
border: 1px solid white;
}
</style>
<title>Online Store</title>
</head>
<body class="container text-center">
<h1>My Tool Store</h1>
<table id="products" class="table table-striped table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
<th>Categories</th>
</tr>
</thead>
<tbody id="insertProducts"></tbody>
</table>
<div class="btn-group" role="group" aria-label="Basic example">
<button id="btn" type="button" class="btn btn-secondary">refresh</button>
<button id="btn-clear" type="button" class="btn btn-secondary">clear</button>
</div>
<!--<form></form>-->
<script src="js/jquery-3.5.1.js"></script>
<script>
(function() {
"use strict";
$().ready(function () {
// TODO: Create an AJAX GET request for the file under data/inventory.json
$.ajax("data/inventory.json")
.done(onSuccess)
.fail(function(jqXhr, status, error) {
alert("There was an error! Check the console for details");
console.log("Response status: " + status);
console.log("Error object: " + error);
});
// TODO: Take the data from inventory.json and append it to the products table
function onSuccess(data, status, jqXhr) {
console.log("Everything went great! Check out the server's response in the console.");
console.log(data);
let html = '';
for (let dataItem of data) {
html += `<tr>
<td class="bg-info">${dataItem["title"]}</td>
<td>${dataItem["quantity"]}</td>
<td class="bg-info">$${dataItem["price"]}</td>
<td>${dataItem["categories"].join(", ")}</td>
</tr>`;
console.log(dataItem);
console.log(dataItem.categories);
console.log(dataItem["categories"]);
}
$('#insertProducts').html(html);
}
$('#btn').click(function () {
$.ajax("data/inventory.json")
.done(onSuccess);
});
$('#btn-clear').click(function () {
$('#insertProducts').empty();
})
})
// 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
})();
</script>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</body>
</html>