This article example shares the specific code of Vue to implement a simple shopping cart for your reference. The specific content is as follows Code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <table> <thead> <tr> <th></th> <th>Book Title</th> <th>Publication Date</th> <th>Price</th> <th>Purchase quantity</th> <th>Operation</th> </tr> </thead> <tbody> <tr v-for='(item,index) in books' ::key="item"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.date}}</td> <td>{{item.price*item.count | getFinalPrice}}</td> <td> <button @click='reduce(index)' :disabled='item.count <= 1'>-</button> {{item.count}} <button @click='add(index)'>+</button> </td> <td> <button @click='removeBtn(index)'>Remove</button> </td> </tr> </tbody> </table> <h2 v-if='books!=""'>Total price:{{theSumOf | getFinalPrice}}</h2> <h2 v-else>Shopping cart is empty</h2> </div> </body> <script src="../js/vue.min.js"></script> <script> var app = new Vue({ el: '#app', data: { books: { id: 1, name: 'Computer Applications', date: '2006-9', price: 85.00, count: 1 }, { id: 2, name: 'java application', date: '2006-9', price: 10.00, count: 1 }, { id: 3, name: 'Big Data', date: '2006-9', price: 85.00, count: 1 }, { id: 4, name: 'ui designer', date: '2006-9', price: 85.00, count: 1 }, ], addAnd:0 }, methods: { add(index) { this.books[index].count++ }, reduce(index) { this.books[index].count-- }, removeBtn(index) { this.books.splice(index, 1) } }, components: }, computed: { theSumOf: function () { //The first method of cumulative calculation //let sum = 0 //this.books.forEach(item => { // sum += parseInt(item.price)*parseInt(item.count) }); //return sum //The second way of cumulative calculation //let sum = 0 //for(let i in this.books){ //sum += this.books[i].price*this.books[i]*count } //return sum //The third way of cumulative calculation //let sum = 0 //for(let item of this.books){ //sum += item.price*item.count //} //return sum //The fourth method of cumulative calculation return this.books.reduce(function(preValue,book){ return preValue + book.price*book.count },0) } }, filters: getFinalPrice(price) { return '¥' + price.toFixed(2) }, } }); </script> <html> Regarding the learning tutorial of vue.js, please click on the special topics vue.js component learning tutorial and Vue.js front-end component learning tutorial for learning. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL 8.0.13 installation and configuration method graphic tutorial under win10
>>: A brief analysis of different ways to configure static IP addresses in RHEL8
Table of contents Preface 1. Routing lazy loading...
The mobile version of the website should at least...
A website uses a lot of HTML5 and CSS3, hoping th...
This article shares a blinds special effect imple...
This article shares the specific code for using j...
ClickHouse is an open source column-oriented DBMS...
Azure Container Registry is a managed, dedicated ...
In order to make the page display consistent betwe...
Table of contents 1. Error phenomenon 2. Error An...
Table of contents 1. What is a database? 2. Class...
Method 1: Use the SET PASSWORD command First log ...
Optimizing and refining information is always the ...
This article shares the detailed steps of install...
Table of contents 1. Scenario 2. Basic functions ...
Pitfalls encountered during project deployment Wh...