vue之最简单的购物车实现

<template>
<div>
  <el-table
    :data="books"
    style="width: 100%">
    <el-table-column
      label="序号"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-star-off"></i>
        <span style="margin-left: 10px">{{ scope.row.id }}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="商品名称"
      width="180">
      <template slot-scope="scope">
        <el-popover trigger="hover" placement="top">
          <p>商品名称: {{ scope.row.title }}</p>
          <p>单价: {{ scope.row.price }}</p>
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.title }}</el-tag>
          </div>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column
      label="单价"
      width="180">
      <template slot-scope="scope">
        <p>{{ scope.row.price }}</p>
      </template>
    </el-table-column>
    <el-table-column
      label="数量"
      width="180">
      <template slot-scope="scope">
        <p>{{ scope.row.count }}</p>
      </template>
    </el-table-column>
    <el-table-column
      label="金额"
      width="180">
      <template slot-scope="scope">
        <p>{{itemPrice(scope.row.price, scope.row.count)}}</p>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          type="primary" plain
          size="mini"
          @click="scope.row.count += 1">加数量</el-button>
          <el-button
          type="primary" plain
          size="mini"
          @click="scope.row.count -= 1">减数量</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="deleteItem(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-divider content-position="right">总价: {{totalPrice}}</el-divider>
</div>
</template>
<script>
export default {
  name: 'shoppingCar',
  props: {
    msg: String
  },
  data(){
    return{
      books:[
        {
          id: 1,
          title: 'Vue.js无难事',
          price: 98,
          count: 1
        },
        {
          id: 2,
          title: 'VC++深入详解',
          price: 168,
          count: 1
        },
        {
          id: 3,
          title: 'Servlet/JSP深入详解',
          price: 139,
          count: 1
        },
      ]
    };
  },
  directives:{
    inserted:function(el) {
      el.focus
    }
  },
  methods: {
    itemPrice(price, count) {
      return price * count;
    },
    deleteItem(index) {
      this.books.splice(index, 1);
    }
  },
  computed: {
    totalPrice() {
      var total = 0;
      for (let book of this.books) {
        total += book.price * book.count;
      }
      return total;
    }
  }
}
</script>

<style scoped>
[v-clock] {
  display: none;
}
</style>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值