-
Notifications
You must be signed in to change notification settings - Fork 0
/
example5.html
285 lines (276 loc) · 7.22 KB
/
example5.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Sub-templates [Ext JS Templating Magic]</title>
<script src="ext-3.3.1/adapter/ext/ext-base.js"></script>
<script src="ext-3.3.1/ext-all.js"></script>
<script src="examples.js"></script>
<link rel="stylesheet" type="text/css" href="ext-3.3.1/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="examples.css" />
<script>
Ext.onReady(function() {
var order = {
"Customer":{
"Id":"BOLID",
"CompanyName":"Bólido Comidas preparadas",
"ContactName":"Martín Sommer",
"ContactTitle":"Owner",
"Address":"C/ Araquil, 67",
"City":"Madrid",
"PostalCode":"28023",
"Country":"Spain",
"Phone":"(91) 555 22 82",
"Fax":"(91) 555 91 99"
},
"CustomerOrders":[
{
"Order":{
"Id":10326,
"CustomerId":"BOLID",
"EmployeeId":4,
"OrderDate":"\/Date(844898400000+0000)\/",
"RequiredDate":"\/Date(847321200000+0000)\/",
"ShippedDate":"\/Date(845244000000+0000)\/",
"ShipVia":2,
"Freight":77.92,
"ShipName":"Bólido Comidas preparadas",
"ShipAddress":"C/ Araquil, 67",
"ShipCity":"Madrid",
"ShipPostalCode":"28023",
"ShipCountry":"Spain"
},
"OrderDetails":[
{
"OrderId":10326,
"ProductId":4,
"UnitPrice":17.6,
"Quantity":24,
"Discount":0
},
{
"OrderId":10326,
"ProductId":57,
"UnitPrice":15.6,
"Quantity":16,
"Discount":0
},
{
"OrderId":10326,
"ProductId":75,
"UnitPrice":6.2,
"Quantity":50,
"Discount":0
}
]
},
{
"Order":{
"Id":10801,
"CustomerId":"BOLID",
"EmployeeId":4,
"OrderDate":"\/Date(883350000000+0000)\/",
"RequiredDate":"\/Date(885769200000+0000)\/",
"ShippedDate":"\/Date(883522800000+0000)\/",
"ShipVia":2,
"Freight":97.09,
"ShipName":"Bólido Comidas preparadas",
"ShipAddress":"C/ Araquil, 67",
"ShipCity":"Madrid",
"ShipPostalCode":"28023",
"ShipCountry":"Spain"
},
"OrderDetails":[
{
"OrderId":10801,
"ProductId":17,
"UnitPrice":39,
"Quantity":40,
"Discount":0.25
},
{
"OrderId":10801,
"ProductId":29,
"UnitPrice":123.79,
"Quantity":20,
"Discount":0.25
}
]
},
{
"Order":{
"Id":10970,
"CustomerId":"BOLID",
"EmployeeId":9,
"OrderDate":"\/Date(890694000000+0000)\/",
"RequiredDate":"\/Date(891900000000+0000)\/",
"ShippedDate":"\/Date(893368800000+0000)\/",
"ShipVia":1,
"Freight":16.16,
"ShipName":"Bólido Comidas preparadas",
"ShipAddress":"C/ Araquil, 67",
"ShipCity":"Madrid",
"ShipPostalCode":"28023",
"ShipCountry":"Spain"
},
"OrderDetails":[
{
"OrderId":10970,
"ProductId":52,
"UnitPrice":7,
"Quantity":40,
"Discount":0.2
}
]
}
]
};
// s sub-template to render the relevant order dates
var orderDateBlockTemplate = new Ext.XTemplate(
'<dl>',
'<dt>Order Date:</dt>',
'<dd>{OrderDate:this.date}</dd>',
'<dt>Required Date:</dt>',
'<dd>{RequiredDate:this.date}</dd>',
'<dt>Shipped Date:</dt>',
'<dd>{ShippedDate:this.date}</dd>',
'</dl>', {
compiled: true,
date: function(d) {
return Date.parseDate(d, 'M$').format('l, F d, Y');
}
});
// a sub-template to render the shipping information
var shippingBlockTemplate = new Ext.XTemplate(
'<p>',
'Shipped via <em>{[this.shipment(values.ShipVia)]}</em> to <address>{ShipName}, {ShipAddress}, {ShipPostalCode} {ShipCity}, {ShipCountry}</address> for {Freight:usMoney}',
'</p>', {
compiled: true,
shipment: function(code) {
switch (code) {
case 1:
return 'air';
case 2:
return 'train';
default:
return 'unknown';
}
}
});
// a mixin that will be used to calculate the sum for an order item
var orderItemCalculatorMixin = {
sum: function(item, withDiscount) {
var sum = item.UnitPrice * item.Quantity
if (withDiscount) {
sum *= (1 - item.Discount);
}
return sum;
}
};
// a single order item row
var orderItemRowTemplate = new Ext.XTemplate(
'<tr class="{[values.xindex % 2 === 0 ? "even" : "odd"]}">',
'<td>{xindex:this.formatNo}.</td>',
'<td><a href="detail.php?id={ProductId}">{ProductId}</a></td>',
'<td>{UnitPrice:usMoney}</td>',
'<td>{Quantity}</td>',
'<td>{Discount*100}%</td>',
'<td>{[fm.usMoney(this.sum(values, true))]}<tpl if="Discount > 0"><br /><small>{[fm.usMoney(this.sum(values, false))]}</small></tpl></td>',
'</tr>', Ext.apply({}, {
compiled: true,
formatNo: function(number) {
return String.leftPad(number.toString(), 5, '0');
}
}, orderItemCalculatorMixin));
// the whole order item table
var orderItemTableTemplate = new Ext.XTemplate(
'<table class="order">',
'<caption>Order items<tpl if="this.isDiscounted(values)"> <em>(discounted)</em></tpl></caption>',
'<thead>',
'<tr>',
'<th>No.</th>',
'<th>Product</th>',
'<th>Unit Price</th>',
'<th>Quantity</th>',
'<th>Discount</th>',
'<th>Sum</th>',
'</tr>',
'</thead>',
'<tbody>',
'<tpl for=".">',
'{[this.orderItemRow(xindex, values)]}',
'</tpl>',
'</tbody>',
'<tfoot>',
'<tr>',
'<th colspan="5">Total</th>',
'<th>{[this.outputOrderSum(values)]}</th>',
'</tr>',
'</tfoot>',
'</table>', Ext.apply({}, {
compiled: true,
orderItemRow: function(xindex, orderItem) {
return orderItemRowTemplate.apply(Ext.apply({}, orderItem, {
xindex: xindex
}));
},
isDiscounted: function(orderItems) {
var isDiscounted = false;
Ext.each(orderItems, function(item) {
if (item.Discount > 0) {
isDiscounted = true;
return false;
}
}, this);
return isDiscounted;
},
outputOrderSum: function(orderItems) {
var sum = 0;
var discountSum = 0;
Ext.each(orderItems, function(item) {
sum += this.sum(item, false);
discountSum += this.sum(item, true);
}, this);
if (sum == discountSum) {
return Ext.util.Format.usMoney(sum);
} else {
return Ext.util.Format.usMoney(discountSum) + '<br /><small>' + Ext.util.Format.usMoney(sum) + '</small>';
}
}
}, orderItemCalculatorMixin));
var template = new Ext.XTemplate(
'<h2>Order Overview</h2>',
'<tpl for=".">',
'<h3>#{[values.Order.Id]}</h3>',
'{[this.orderDateBlock(values.Order)]}',
'{[this.shippingBlock(values.Order)]}',
'{[this.orderItemTable(values.OrderDetails)]}',
'</tpl>', {
compiled: true,
orderDateBlock: function(order) {
return orderDateBlockTemplate.apply(order);
},
shippingBlock: function(order) {
return shippingBlockTemplate.apply(order);
},
orderItemTable: function(orderItems) {
return orderItemTableTemplate.apply(orderItems);
}
});
template.append('content', order.CustomerOrders);
});
</script>
</head>
<body id="example5">
<ul id="page-nav">
<li><a rel="index" href="#">index</a></li>
<li><a rel="prev" href="#">←</a></li>
<li><a rel="next" href="#">→</a></li>
</ul>
<h1><a rel="source" href="#">view source</a>Sub-templates</h1>
<div id="content"></div>
<div id="footer">
Stefan Gehrig, 2011, to accompany the article "Ext JS templating magic" in <a href="http://jsmag.com"><jsmag></a> 05/2011
</div>
</body>
</html>