/
ajax-store.html
112 lines (88 loc) · 2.87 KB
/
ajax-store.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
<!DOCTYPE html>
<html>
<head>
<title>Online Store</title>
<link rel="stylesheet" href='css/bootstrap.css'>
</head>
<body>
<main class="container">
<h1>My Tool Store</h1>
<table id="products" class="table">
<thead>
<tr>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
<th>Categories</th>
</tr>
</thead>
<tbody id="insertProducts"></tbody>
</table>
</main>
<script src="js/jquery-3.0.0.js"></script>
<script src="js/bootstrap.js"></script>
<script>
// TODO: Create an ajax GET request for /data/inventory.json
"use strict";
//main work done
var invRequest = $.get('data/inventory.json');
invRequest.done(function (response) {
$('#insertProducts').html(buildTableRows(response));
}).fail(function (response) {
$('#insertProducts').html('fail: ' + response);
});
function buildTableRows(invArray) {
var tableRow = '';
invArray.forEach(function (invItem) {
tableRow += '<tr>'
+ '<td>' + invItem.title + '</td>'
+ '<td>' + invItem.quantity + '</td>'
+ '<td>' + invItem.price + '</td>'
+ '<td>' + invItem.categories.join(', ') + '</td>'
+ '</tr>'
});
return tableRow;
}
/* review example 1
var ajaxObject = $.get('data/inventory.json');
ajaxObject.done(function (response) {
console.log(response);
var htmlContent = '';
response.forEach(function (tool) {
htmlContent += '<tr><td>'
+ tool.title + '</td><td>'
+ tool.quantity + '</td><td>'
+ tool.price + '</td><td>'
+ tool.categories + '</td></tr>'
});
$('#insertProducts').append(htmlContent);
});
ajaxObject.fail(function (error) {
console.log('fail: ' + error);
});
*/
/* review example 2
var ajaxObject = $.get('data/inventory.json');
ajaxObject.done(function (response) {
console.log(response);
var htmlContent = '';
response.forEach(function (tool) {
$('#insertProducts').append('<tr><td>'
+ tool.title + '</td><td>'
+ tool.quantity + '</td><td>'
+ tool.price + '</td><td>'
+ tool.categories + '</td></tr>'
);
});
ajaxObject.fail(function (error) {
console.log('fail: ' + error);
});
});
*/
// 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
</script>
</body>
</html>