Vue
HTML
<div id="app">
<table>
<thead>
<tr>
<th>Product</th>
<th>Unit price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr v-for="(cartItem, index) in order.cartItems">
<td>{{ cartItem.productName }}</td>
<td>{{ cartItem.unitPrice }}</td>
<td>{{ cartItem.quantity }}</td>
<td>{{ lineTotal[index] }}</td>
</tr>
<tr class="shipping">
<td>Shipping <small>(1.50 per item)</small></td>
<td>{{ shipping }}</td>
<td>{{ totalItems }}</td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">TOTAL</td>
<td></td>
</tr>
</tfoot>
</table>
</div>
CSS
body {
font-family: sans-serif;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 15px;
border: 1px solid #000;
}
thead th {
background: #aaa;
font-weight: bold;
}
tbody td {
background: #eee;
}
tbody .shipping td {
background: #ddd;
}
tfoot td {
background: #aaa;
font-weight: bold;
}
Vue
const shipping = 1.5;
const app = new Vue({
el: '#app',
data: {
shipping: shipping.toFixed(2),
order: []
},
created() {
fetch('https://api.myjson.com/bins/1fk6ua')
.then(response => response.json())
.then(json => {
this.order = json.order
})
},
computed: {
lineTotal: function () {
return this.order.cartItems.map(function (cartItem) {
return (cartItem.unitPrice * cartItem.quantity).toFixed(2);
});
},
totalItems: function(){
return this.order.cartItems.reduce(function (totalItems, cartItem) {
return totalItems + parseInt(cartItem.quantity);
}, 0);
}
}
});