Day16——书籍购物车案例

一. 回顾

前面学习了插值操作、mustache语法、v-once、v-html、v-text、动态绑定v-bind、v-bind绑定class对象以及数组、v-bind绑定style对象以及数组、计算属性computed、ES6语法、v-on事件监听、v-if条件判断、v-for循环遍历、响应式的数组方法。今天对这些已学知识作出总结并写一个案例。

二. 书籍购物车案例

效果:
在这里插入图片描述
项目目录:
在这里插入图片描述

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>index</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>

<div id="app" v-cloak>
  <div v-if="list.length">
    <table>
      <thead>
        <tr>
          <th></th>
          <th>书籍名称</th>
          <th>出版日期</th>
          <th>价格</th>
          <th>购买数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in list" :key="item.id">
          <td>{{index+1}}</td>
          <td>{{item.name}}</td>
          <td>{{item.date}}</td>
          <!-- '| 方法名'是使用过滤方法的意思-->
          <td>{{item.price | showPrice}}</td>
          <td>
            <button @click="decrement(index)" :disabled="item.count===1">-</button>
            {{item.count}}
            <button @click="increment(index)">+</button>
          </td>
          <td>
            <button @click="handleRemove(index)">移除</button>
          </td>
        </tr>
      </tbody>
    </table>
    <div>总价: {{totalPrice | showPrice}}</div>
  </div>
  <div v-else>购物车为空</div>
</div>

<script src="../js/vue.js"></script>
<script src="index.js"></script>
</body>
</html>


style.css

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;
}

index.js

const app = new Vue({
	el: '#app',
	data: {
		list: [
			{
				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
			},
		]
	},
	methods: {
		decrement(index) {
			this.list[index].count--;
		},
		increment(index) {
			this.list[index].count++;
		},
		handleRemove(index) {
			this.list.splice(index, 1);
		}
	},
	filters: {
		showPrice(value) {
			return '¥' + value.toFixed(2);//toFixed(2)是保留2位小数的意思
		}
	},
	computed: {
		totalPrice() {
			let total = 0;
      /**
       * 普通循环
       * @type {number}
       */
			/*for (let i = 0; i < this.list.length; i++) {
				let item = this.list[i];
				total += item.price * item.count;
			}*/

      //1. for(let i in this.list)
      /*for(let i in this.list){
        total += this.list[i].count * this.list[i].price;
      }*/

      //2. for(let i of this.list)
      /*for(let i of this.list){
        total += i.count * i.price;
      }
			return total*/

      //3. 使用高阶函数直接返回结果
      return this.list.reduce(function (prevalue, listElement) {
        return prevalue + listElement.count * listElement.price;
      }, 0)
		}
	}


})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值