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

Commonplace talk about MySQL event scheduler (must read)

Overview MySQL also has its own event scheduler, ...

Mysql sorting and paging (order by & limit) and existing pitfalls

Sorting query (order by) In e-commerce: We want t...

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

Problems and solutions when installing MySQL8.0.13 on Win10 system

Operating system: Window10 MySQL version: 8.0.13-...

Two ways to build Docker images

Table of contents Update the image from an existi...

MySQL Workbench download and use tutorial detailed explanation

1. Download MySQL Workbench Workbench is a graphi...

How to use VUE and Canvas to implement a Thunder Fighter typing game

Today we are going to implement a Thunder Fighter...

How to implement Vue binding class and binding inline style

Table of contents Binding Class Binding inline st...

Nginx local directory mapping implementation code example

Sometimes you need to access some static resource...

8 JS reduce usage examples and reduce operation methods

reduce method is an array iteration method. Unlik...

MySQL 5.7 JSON type usage details

JSON is a lightweight data exchange format that u...