-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjex example.html
More file actions
195 lines (172 loc) · 5.25 KB
/
Ajex example.html
File metadata and controls
195 lines (172 loc) · 5.25 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!--<!DOCTYPE html>-->
<!--<html>-->
<!--<head>-->
<!-- <title>Orders</title>-->
<!-- <link rel="shortcut icon" href="about:blank">-->
<!-- <style>-->
<!-- article {-->
<!-- background-color: cornsilk;-->
<!-- border: 2px solid black;-->
<!-- margin-bottom: 5px;-->
<!-- padding: 10px;-->
<!-- }-->
<!-- dt {-->
<!-- font-weight: bold;-->
<!-- }-->
<!-- dd {-->
<!-- text-decoration: underline;-->
<!-- }-->
<!-- </style>-->
<!--</head>-->
<!--<body>-->
<!--<h1>Current Orders</h1>-->
<!--<div id="orders">-->
<!-- <!– This HTML should be dynamically generated by JS –>-->
<!-- <article>-->
<!-- <h3>Order #x</h3>-->
<!-- <dl>-->
<!-- <dt>Item Ordered</dt>-->
<!-- <dd>Such and such item</dd>-->
<!-- <dt>Ordered by</dt>-->
<!-- <dd>Ordered by such and such</dd>-->
<!-- </dl>-->
<!-- </article>-->
<!--</div>-->
<!--<button id="update">Update w/Ajax</button>-->
<!--<script src="https://code.jquery.com/jquery-3.6.3.js" integrity="sha256-nQLuAZGRRcILA+6dMBOvcRh5Pe310sBpanc6+QBmyVM=" crossorigin="anonymous"></script>-->
<!--<script>-->
<!-- $(document).ready(function() {-->
<!-- "use strict";-->
<!-- /*-->
<!-- 1. make a request for the data (orders.json)-->
<!-- 2. loop through the data and build an HTML string to create the HTML to display the data-->
<!-- 3. update the DOM with the new HTML-->
<!-- */-->
<!-- /**-->
<!-- * Creates an HTML string based on a JSON array of orders-->
<!-- * @param orders-->
<!-- */-->
<!-- /*-->
<!-- {-->
<!-- "orderNumber": 1,-->
<!-- "itemOrdered": "Small Sweater",-->
<!-- "orderedBy": "Johnny Be Good"-->
<!-- }-->
<!-- */-->
<!-- $.ajax('./data/orders.json').done(function(data) {-->
<!-- // console.log(data);-->
<!-- let html = '';-->
<!-- for (let i = 0; i < data.length; i += 1) {-->
<!-- html += `<article>-->
<!-- <h3>Order #${data[i].orderNumber}</h3>-->
<!-- <dl>-->
<!-- <dt>Item Ordered</dt>-->
<!-- <dd>${data[i].itemOrdered}</dd>-->
<!-- <dt>Ordered by</dt>-->
<!-- <dd>${data[i].orderedBy}</dd>-->
<!-- </dl>-->
<!-- </article>`;-->
<!-- }-->
<!-- $('#orders').html(html);-->
<!-- });-->
<!-- });-->
<!--</script>-->
<!--</body>-->
<!--</html>-->
<!DOCTYPE html>
<html>
<head>
<title>Orders</title>
<link rel="shortcut icon" href="">
<style>
.card {
background-color: cornsilk;
border: 2px solid black;
margin-bottom: 5px;
padding: 10px;
}
dt {
font-weight: bold;
}
dd {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Current Orders</h1>
<div id="orders">
<!-- This HTML should be dynamically generated by JS -->
<article>
<h3>Order #x</h3>
<dl>
<dt>Item Ordered</dt>
<dd>Such and such item</dd>
<dt>Ordered by</dt>
<dd>Ordered by such and such</dd>
</dl>
</article>
</div>
<button id="update">Update w/Ajax</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";
/*
1. make a request for the data (orders.json)
2. loop through the data and build an HTML string to create the HTML to display the data
3. update the DOM with the new HTML
*/
/**
* Creates an HTML string based on a JSON array of orders
* @param orders
*/
/*
{
"orderNumber": 1,
"itemOrdered": "Small Sweater",
"orderedBy": "Johnny Be Good"
}
*/
function createHTML(orders) {
// loop through all the orders and create an HTML string for each order
let html = '';
for (let i = 0; i < orders.length; i += 1) {
html += ' <article class="card">\n' +
' <h3>Order #' + orders[i].orderNumber + '</h3>\n' +
' <dl>\n' +
' <dt>Item Ordered</dt>\n' +
' <dd>' + orders[i].itemOrdered + '</dd>\n' +
' <dt>Ordered by</dt>\n' +
' <dd>' + orders[i].orderedBy + '</dd>\n' +
' </dl>\n' +
' </article>';
}
return html;
}
/**
* Triggers a new AJAX request to the orders.json file and re-renders the orders HTML using the createHTML function
*/
function updatePage() {
// make request to orders.json
console.log('Update orders');
$.get('./data/orders.json').done(function(orders) {
let ordersHTML = createHTML(orders); // create HTML string
$('#orders').html(ordersHTML); // update the DOM with the new HTML
}).fail(function(jqXhr, status, error) {
console.log(jqXhr);
console.log(status);
console.log(error);
});
}
updatePage();
// when a user clicks on the update button, refresh the page with the latest order information
$('#update').on('click', function() {
updatePage();
});
// every 3 seconds, update the orders to show the latest changes
setInterval(updatePage, 3000);
});
</script>
</body>
</html>