美文网首页
图书购物车

图书购物车

作者: 小圆圈Belen | 来源:发表于2022-02-24 17:25 被阅读0次

显示图书购物车界面,移除所有书籍之后,显示购物车为空


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>v-bind</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/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: #f7f7f7;
      color: #5c6b77;
      font-weight: 600;
    }
  </style>
</head>

<!--需求描述:图书购物车-->
<body>
<div id= 'vue' >
  <div v-if="books.length>0">
  <table>
    <thead>
    <tr>
      <th></th>
      <th>书籍名称</th>
      <th>出版日期</th>
      <th>价格</th>
      <th>购买数量</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr v-for="(item,index) in books"  :key="item.id">
      <th>{{item.id}}</th>
      <th>{{item.name}}</th>
      <th>{{item.date}}</th>
<!--      <th>{{getFinalPrice(item.price)}}</th>-->
      <th>{{item.price | showPrice}}</th>
      <th>
        <button @click="sub(index)" :disabled="item.count<=1">-</button>
        {{item.count}}
        <button @click="add(index)">+</button>
      </th>
      <th>
        <button @click="remove(index)">移除</button>
      </th>
    </tr>
    </tbody>
  </table>
    <h4>总价:{{totalPrice | showPrice}}</h4>
  </div>
  <h3 v-else>购物车为空</h3>

</div>
<script>
  let vm = new Vue({
    el: '#vue',
    data:{
      books: [
        {
          id: 1,
          name: '算法导论',
          date: '2006 - 9',
          price: 85.00,
          count: 1,
        },
        {
          id: 2,
          name: 'UNIX编程艺术',
          date: '2006 - 2',
          price: 59.00,
          count: 1,
        },
        {
          id: 3,
          name: '编程珠玑',
          date: '2008 - 10',
          price: 39.00,
          count: 1,
        },
        {
          id: 4,
          name: '代码大全',
          date: '2006 - 3',
          price: 128.00,
          count: 1,
        }
      ],
      sum: 0
    },
    methods:{
      getFinalPrice(price){
        return '¥  '+ price.toFixed(2)
      },
      add(index){
        this.books[index].count++
      },
      sub(index){
        this.books[index].count--
      },
      remove(index){
        this.books.splice(index,1),
        console.log('移除了第' + index + '个元素')
      }
      },
    computed:{
      totalPrice(){
        let sum = 0
        for(let i=0;i<this.books.length;i++){
          sum += this.books[i].price * this.books[i].count
        }
        return sum
      }
    },
    filters:{
      showPrice(price){
        return '¥  '+ price.toFixed(2)
      }
    }
  })
</script>
</body>
</html>

相关文章

  • 图书购物车

    显示图书购物车界面,移除所有书籍之后,显示购物车为空

  • Javaweb-案例练习-4-加入购物车的实现过程

    添加购物车功能实现 这篇来做一个练习,就是图书详情页面,点击购买按钮,这本书添加到购物中。购物车中主要思想就是采用...

  • React图书购物车案例练习

    功能 1.在界面上以表格的形式,显示一些书籍的数据;2.在底部显示书籍的总价格;3.点击+或者-可以增加或减少书籍...

  • 《当我谈跑步时  我谈些什么》

    这本书很难得,当当网购物车里躺了很久,在图书馆里找了好些日子才找到,因为对图书馆的使用方法不熟,现在渐渐摸清了路,...

  • 《美与幼童》——绘本的看看看

    忘记在微博还是哪里看过《美与幼童》的图书推荐,随手加入购物车,等待哪次购入。恰巧中午在单位看书,随意翻阅,...

  • 在线书店的分析

    一、模块划分 1.用户(注册、登录、个人账户管理-收货人管理) 2 图书(增 删 改 查) 3 购物车(添加、修改...

  • 商城之购物车

    购物车管理: 包含功能:提交商品到购物车、显示购物车列表、删除购物车里商品、修改购物车、清空购物车等等 1、购...

  • 购物车模块实现

    1、购物车列表功能实现 点击加入购物车或者点击购物车图标后进入购物车页面,在购物车页面中首先渲染cartList的...

  • 一个美丽的错误,换一个美丽的故事丨人生海海 ,踏浪前行

    麦家的书《人生海海》最近频繁在各大畅销榜出现,简单看了介绍,便放入了购物车。 一日,采购其他图书时,蹦出提示,满足...

  • SSM框架学习日记(6)——购物车模块

    购物车相关接口 添加购物车,购物车列表,更新商品数量,删除购物车先新建CartController和CartSer...

网友评论

      本文标题:图书购物车

      本文链接:https://www.haomeiwen.com/subject/dcbzlrtx.html