A Pen by Edward Lance Lorilla on CodePen.
Created
June 2, 2021 16:34
-
-
Save edwardlorilla/8a8d1b9146376e43a46dca0ff5d2cd2a to your computer and use it in GitHub Desktop.
GRWQEgZ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app" v-cloak> | |
<div v-if="books.length"> | |
<table> | |
<thead> | |
<tr> | |
<th></th> | |
<th>Book name</th> | |
<th>Publication date</th> | |
<th>Price</th> | |
<th>Purchase quantity</th> | |
<th>Delete</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr v-for="(item,index) in books"> | |
<th>{{item.id}}</th> | |
<th>{{item.name}}</th> | |
<th>{{item.date}}</th> | |
<!--Scheme 1 Keep the decimal point and currency symbol--> | |
<!-- <th>{{"$"+item.price.toFixed(2)}}</th> --> | |
<!--Scheme Two--> | |
<!-- <th>{{getFinalPrice(item.price)}}</th> --> | |
<!--Plan Three--> | |
<th>{{item.price | showPrice}}</th> | |
<th> | |
<button @click="decrement(index)" :disabled="item.count<=0">-</button> | |
{{item.count}} | |
<button @click="increment(index)">+</button> | |
</th> | |
<th><button @click="removeHandle(index)">Remove</button></th> | |
</tr> | |
</tbody> | |
</table> | |
<h2>Total price: {{totalPrice | showPrice}}</h2> | |
</div> | |
<h2 v-else> | |
Shopping cart is empty | |
</h2> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const app = new Vue({ | |
el:"#app", | |
data:{ | |
books:[ | |
{ | |
id:1, | |
name:"Introduction to Algorithms", | |
date:'2006-9', | |
price: 85.00, | |
count:1 | |
}, | |
{ | |
id:2, | |
name:"UNIX Programming Art", | |
date:'2006-2', | |
price: 50.00, | |
count:1 | |
}, | |
{ | |
id:3, | |
name:"Programming Art", | |
date:'2008-10', | |
price:39.00, | |
count:1 | |
}, | |
{ | |
id:4, | |
name:"Code Encyclopedia", | |
date:'2006-3', | |
price:128.00, | |
count:1 | |
}, | |
] | |
}, | |
methods: { | |
//Here we abandon the use of methods to find the total price, and instead use calculated attributes because it is more efficient. | |
// getFinalPrice(price){ | |
// return "¥"+price.toFixed(2) | |
// }, | |
increment(index){ | |
this.books[index].count++ | |
}, | |
decrement(index){ | |
this.books[index].count-- | |
}, | |
removeHandle(index){ | |
this.books.splice(index,1); | |
} | |
}, | |
computed: { | |
totalPrice(){ | |
// Option 1: Ordinary for loop | |
// let totalPrice = 0; | |
// for(let i=0;i<this.books.length;i++){ | |
// totalPrice += this.books[i].price * this.books[i].count | |
//} | |
// return totalPrice | |
// Option 2: for in | |
// let totalPrice = 0; | |
// for(let i in this.books){ | |
// // console.log(i);//1 2 3 4 | |
// totalPrice += this.books[i].price * this.books[i].count | |
//} | |
// return totalPrice | |
// Option 3: for of | |
// let totalPrice = 0; | |
// for(let item of this.books){ | |
// // console.log(item);//What you get here is each object in the array | |
// totalPrice += item.price * item.count | |
//} | |
// return totalPrice | |
// Option 4: reduce | |
return this.books.reduce(function (preValue, book) { | |
// console.log(book);//output four objects separately | |
return preValue + book.price * book.count | |
}, 0) | |
} | |
}, | |
// filter | |
filters:{ | |
showPrice(price){ | |
return "$"+price.toFixed(2) | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table{ | |
border: 1px solid #e9e9e9; | |
border-collapse: collapse; | |
border-spacing: 0; | |
} | |
th,td{ | |
padding: 8px 16px; | |
border: 1px solid #e9e9e9; | |
text-align: center; | |
} | |
th{ | |
background-color: #f7f7f7; | |
color: #5c6b77; | |
font-weight: 600; | |
} | |
[v-cloak]{ | |
display: none; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment