Vuejs Realization of Shopping cart function

Source: Internet
Author: User

<body>
<H1 align= "center" > Shopping Cart instance <div id= "VM1" >
<table align= "center" width= "border=" 1 "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
<th> Serial Number </th>
<th> Products </th>
<th> Unit Price </th>
<th> Quantity </th>
<th> Subtotal </th>
<th> Operations </th>
</tr>
<tr v-for= "(product,index) in Productslist" >
<td>{{number (Index) +1}}</td>
<td>{{product.name}}</td>
<TD class= "Money" >{{product.price}}</td>

<td>
<button v-on:click= "Jian (index)" >-</button>

<input type= "text" v-model= "Product.num" v-on:input= "Change (index)" >

<button v-on:click= "Add (Index)" >+</button>
</td>
<TD class= "Money" >{{product.price*product.num}}</td>
<td><button v-on:click= del (index) > Remove cart </button></td>
</tr>
</table>

<table align= "center" width= "border=" 1 "cellpadding=" 5 "cellspacing=" 0 "style=" margin-top:60px; " >
<tr>
<th> total number of items purchased </th>
<th> Total </th>
</tr>
<tr>
<td>{{Count2 ()}}</td>
&LT;TD class= "Money" >{{Allmoney ()}}</td>

</tr>
</table>
</div>

<script>
var vm1=new Vue ({
El: ' #vm1 ',
data:{
productslist:[
{ID: ' 0 ', Name: ' IPhone 6 Plus ', Price: ' + ', num:1},
{ID: ' 1 ', Name: ' Computer table ', Price: ' + ', num:1},
{ID: ' 2 ', Name: ' Tablet ', Price: ' + ', num:1}
]
},
methods:{
Delete
Del:function (index) {
if (Confirm ("Are you sure you want to delete it?") ")){
This.productsList.splice (index,1);
}
},
Increase
Add:function (index) {
if (this.productslist[index].num<100) {
this.productslist[index].num++;
}
},
Reduce
Jian:function (index) {
if (this.productslist[index].num>1) {
this.productslist[index].num--;
}
},
Modify
Change:function (index) {
if (this.productslist[index].num>100) {
this.productslist[index].num=100;
}else if (this.productslist[index].num<1) {
This.productslist[index].num=1;
}
},
Total quantity
Count2:function () {
var list=this.productslist;
var total=0;
for (Var i=0;i<list.length;i++) {
Total+=parseint (List[i].num);
}
return total;
},

Total Amount
Allmoney:function () {
var list=this.productslist;
var totalmoney=0;
for (Var i=0;i<list.length;i++) {
Totalmoney+=parseint (list[i].num) *list[i].price;
}
return Totalmoney;
}


}
});


</script>
</body>

Vuejs Realization of Shopping cart function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.