vue+antd实现购物车

使用:vue框架+antd第三方组件

<template>
	    <div class="shopping-card">
      <div class="card-title">
        我的购物车
      </div>
      <div class="card-table">
        <a-table
          :row-selection="{
            selectedRowKeys: selectedRowKeys,
            onChange: onSelectChange
          }"
          :columns="columns"
          :data-source="shoppingData"
          :pagination="false"
        >
          <div style="width: 150px" slot="num" slot-scope="text, record">
            <a-button :disabled="record.num <= 1" @click="spliceNum(record.id)">
              -
            </a-button>
            <a-input style="width: 40px" placeholder="1" v-model="record.num" />
            <a-button @click="addNum(record.id)">
              +
            </a-button>
          </div>
          <div
            style="width: 50px;cursor: pointer"
            slot="action"
            slot-scope="text, record"
          >
            <div @click="deleteShopping(record.id)">删除</div>
          </div>
        </a-table>
      </div>
      <div class="card-end">
        总计:<span style="color: red;font-size: 20px">{{ totalPrice }}</span
        ></div>
    </div>
</template>

<script>
export default {
	data() {
		columns: [
	        {
	          title: "商品名称",
	          key: "name",
	          dataIndex: "name",
	          align: "center"
	        },
	        {
	          title: "商品单价",
	          key: "price",
	          dataIndex: "price",
	          align: "center"
	        },
	        {
	          title: "购买数量",
	          key: "num",
	          dataIndex: "num",
	          slots: { title: "num" },
	          scopedSlots: { customRender: "num" },
	          align: "center"
	        },
	        {
	          title: "小计",
	          key: "",
	          dataIndex: "singerNum",
	          align: "center"
	        },
	        {
	          title: "操作",
	          key: "action",
	          dataIndex: "action",
	          slots: { title: "action" },
	          scopedSlots: { customRender: "action" },
	          align: "center"
	        }
	     ],
	           shoppingData: [
        {
          id: 0,
          name:
            "极米 (XGIMI )Play特别版 投影机 投影仪 家用 便携(单手可握 侧投 HDR10解码哈曼卡顿音响内置电池)",
          price: 2499,
          num: 1,
          singerNum: 2499
        },
        {
          id: 1,
          name:
            "荣耀MagicBook 2019 14英寸轻薄窄边框笔记本电脑(AMD锐龙7 3700U 8G 512G FHD IPS 指纹解锁)冰河银",
          price: 4985,
          num: 1,
          singerNum: 4985
        },
        {
          id: 2,
          name: "盼盼 手撕面包 饼干蛋糕整箱大礼包箱装 奶香味2000g",
          price: 52.5,
          num: 1,
          singerNum: 52.5
        },
        {
          id: 3,
          name: "洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶洗面奶",
          price: 56,
          num: 1,
          singerNum: 56
        },
        {
          id: 4,
          name:
            "联想 K5 Note 4GB+64GB 6英寸全面屏双摄手机 全网通 移动4G+ 双卡双待 极地黑",
          price: 999,
          num: 1,
          singerNum: 999
        },
        {
          id: 5,
          name:
            '小米MIX2 全面屏游戏手机 6GB+64GB 黑色 全网通4G手机 双卡双待 5.99"大屏',
          price: 1299,
          num: 1,
          singerNum: 1299
        },
        {
          id: 6,
          name:
            "Apple MacBook Pro 15.4英寸笔记本电脑 银色(2017款Multi-Touch Bar/Core",
          price: 15888,
          num: 1,
          singerNum: 15888
        },
        {
          id: 7,
          name:
            "OPPO R15 全面屏双摄拍照手机 4G+128G 幻色粉 全网通 移动联通电信",
          price: 2799,
          num: 1,
          singerNum: 2799
        }
      ],
      selectedRowKeys: [], // Check here to configure the default column
      totalPrice: 0
	},
	methods: [
	  onSelectChange(selectedRowKeys) {
	      this.totalPrice = 0
	      console.log("selectedRowKeys changed: ", selectedRowKeys);
	      this.selectedRowKeys = selectedRowKeys;
	      this.shoppingData.map(shopping => {
	        this.selectedRowKeys.map(selected => {
	          if(shopping.id == selected) {
	            this.totalPrice = this.totalPrice + shopping.singerNum
	          }
	        })
	        return shopping
	      })
    },
    spliceNum(id) {
      this.totalPrice = 0
      console.log(id);
      this.shoppingData.map(shopping => {
        if (shopping.id == id) {
          --shopping.num;
          shopping.singerNum = shopping.num * shopping.price;
        }
        this.onSelectChange(this.selectedRowKeys)
        return shopping;
      });
    },
    addNum(id) {
      this.totalPrice = 0
      console.log(id);
      this.shoppingData.map(shopping => {
        if (shopping.id == id) {
          ++shopping.num;
          shopping.singerNum = shopping.num * shopping.price;
        }
        this.onSelectChange(this.selectedRowKeys)
        return shopping;
      });
    },
    deleteShopping(id) {
      this.shoppingData.map((shopping,index) => {
        if (shopping.id == id) {
          this.shoppingData.splice(index, 1);
        }
      });
      this.onSelectChange(this.selectedRowKeys);
    }
	]
}
</script>

<style scoped>
.shopping-card {
  margin: 50px;
  border: 1px solid #c2bbbb;
}
.card-title {
  text-align: left;
  height: 50px;
  line-height: 50px;
  background: #c2bbbb;
}
.card-end {
  text-align: right;
  height: 50px;
  line-height: 50px;
  background: #c2bbbb;
  padding-right: 20px;
}
</style>

实现效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值