Vue implements a simple shopping cart example

Vue implements a simple shopping cart example

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:
  • Vuejs teaches you step by step to write a complete shopping cart example code
  • Implementing shopping cart function based on Vuejs
  • Vue implements shopping cart function
  • Vue implements a small case of shopping cart
  • Vue realizes the shopping cart function of the mall
  • Simple shopping cart function example implemented by vuex
  • Vue realizes shopping cart total price calculation
  • vue+vant-UI framework realizes the checkbox selection and deselection function of the shopping cart
  • Vuejs implements shopping cart function
  • Detailed explanation of the shopping cart function implemented by Vue.js

<<:  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

Recommend

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

Some conclusions on developing mobile websites

The mobile version of the website should at least...

Native JS to achieve blinds special effects

This article shares a blinds special effect imple...

js to achieve cool fireworks effect

This article shares the specific code for using j...

Tutorial on how to quickly deploy clickhouse using docker-compose

ClickHouse is an open source column-oriented DBMS...

Issues with using Azure Container Registry to store images

Azure Container Registry is a managed, dedicated ...

CSS style reset and clear (to make different browsers display the same effect)

In order to make the page display consistent betwe...

Solution to the error in compiling LVGL emulator on Linux

Table of contents 1. Error phenomenon 2. Error An...

Let's learn about MySQL database

Table of contents 1. What is a database? 2. Class...

4 ways to modify MySQL root password (summary)

Method 1: Use the SET PASSWORD command First log ...

CentOS installation mysql5.7 detailed tutorial

This article shares the detailed steps of install...

JavaScript Sandbox Exploration

Table of contents 1. Scenario 2. Basic functions ...

How to set up jar application startup on CentOS7

Pitfalls encountered during project deployment Wh...