Vue shopping cart case study

Vue shopping cart case study

1. Shopping cart example

After a series of studies, let's practice a shopping cart case here.

Requirements: Use vue to write a form page with the purchase quantity on the page. Click the + or - button to increase or decrease the number of shopping carts. The minimum quantity must not be less than 0. Click the remove button to remove the product. When all products are removed, the form on the page disappears.

Then the text appears: Shopping cart is empty. Below the form is the total price of the goods, which increases as the number of goods increases. The default is 0 yuan.

The overall effect is as follows:

2. Code Implementation

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="../js/vue.js"></script>
  <style>
    table{
        border: 1px solid #e9e9e9;
        border-collapse: collapse;
        border-spacing: 0;
    }
    th,td{
        padding: 8px 16px;
        border: 1px solid #e9e9e9;
        text-align: left;
    }
    th{
        background-color: #f7f7f7;
        color: #5c6b77;
        font-weight: 600;
    }
  </style>
</head>
<body>
<div id="app">
  <div v-if="books.length">
    <table>
      <thread>
        <tr>
          <th></th>
          <th>Book Title</th>
          <th>Publication Date</th>
          <th>Price</th>
          <th>Purchase quantity</th>
          <th>Operation</th>
        </tr>
      </thread>
      <tbody>
      <tr v-for="(book, index) in books" :key="book">
        <td>{{index+1}}</td>
        <td>{{book.name}}</td>
        <td>{{book.publish_date}}</td>
        <td>{{book.price | showPrice}}</td>
        <td>
          <button @click="decrease(index)" :disabled="book.count <= 0">-</button>
          {{book.count}}
          <button @click="increase(index)">+</button>
        </td>
        <td>
          <button @click="removeClick(index)">Remove</button>
        </td>
      </tr>
      </tbody>
    </table>
    <p>Total price: {{totalPrice | showPrice}}</p>
  </div>
  <h2 v-else>Shopping cart is empty</h2>
</div>
<script>
  const app = new Vue({
    el: "#app",
    data: {
      books:
        {"name":"Introduction to Algorithms", "publish_date":"2006-9", "price":20.00, "count": 0},
        {"name":"The Art of UNIX Programming", "publish_date":"2006-2", "price":30.00, "count": 0},
        {"name":"Programming Technology", "publish_date":"2008-10", "price":40.00, "count": 0},
        {"name":"Code Collection", "publish_date":"2006-3", "price":50.00, "count": 0},
      ],
    },
    methods: {
      // Increase +
      decrease(index){
        this.books[index].count-=1
      },
      // reduce-
      increase(index){
        this.books[index].count+=1
      },
      //Remove button removeClick(index){
        this.books.splice(index, 1)
      }
    },
    computed: {
      // Calculate the total price totalPrice(){
        let totalPrice = 0
        for (let item of this.books){
          totalPrice += item.price * item.count
        }
        return totalPrice
      }
    },
    // Filters to filter prices to 2 decimal places filters: {
      showPrice(price){
        return '¥' + price.toFixed(2)
      }
    }
  })
</script>
</body>
</html>

3. Summary

v-for: loop, loop books list
v-on: event monitoring, monitoring click events
disabled: The attribute of whether the button is clickable. If it is True , it is clickable, and if it is False , it is not clickable. Add judgment conditions: disabled="book.count <= 0" When the number of shopping carts ≤0 , it cannot be clicked
v-if and v-else: conditional judgment, judge the length of the books list, if there is a length, display the list, if the length is 0, display the text shopping cart empty
Filters: Custom filter, filter the price, so that the price has 2 decimal places after filtering
computed: computed property, calculate the total price of the purchase

This is the end of this article about Vue shopping cart case practice. For more relevant Vue shopping cart practice content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A small exercise to implement shopping cart in Vue

<<:  Details of various font formats in HTML web pages

>>:  Introduction to CSS foreground and background automatic color matching technology (demo)

Recommend

MySQL daily statistics report fills in 0 if there is no data on that day

1. Problem reproduction: Count the total number o...

Creative opening effect achieved by combining CSS 3.0 with video

Let me share with you a creative opening realized...

How to use resize to implement image switching preview function

Key Points The CSS resize property allows you to ...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

uniapp implements date and time picker

This article example shares the specific code of ...

Summary of commonly used SQL statements for creating MySQL tables

Recently, I have been working on a project and ne...

Implementation of Nginx operation response header information

Prerequisite: You need to compile the ngx_http_he...

Detailed explanation of how MySQL (InnoDB) handles deadlocks

1. What is deadlock? The official definition is a...

Summary of two methods to implement vue printing function

Method 1: Install the plugin via npm 1. Install n...

The use of textarea in html and common problems and case analysis

The textarea tag is an HTML tag that we often use....

NodeJs high memory usage troubleshooting actual combat record

Preface This is an investigation caused by the ex...

A brief discussion on MySQL user permission table

MySQL will automatically create a database named ...

Element-ui directly clicks on the cell in the table to edit

Table of contents Achieve results Implementation ...

Detailed explanation of nginx request header data reading process

In the previous article, we explained how nginx r...

How to clean up data in MySQL online database

Table of contents 01 Scenario Analysis 02 Operati...