vue+element 实现购物车功能

全选,多选,价格计算,开箱即用(没scss的安装下scss )

在这里插入图片描述
在这里插入图片描述

template

<div class="cart-group">
        <div class="sellect-all">
          <div class="radio">
            <el-checkbox :indeterminate="isIndeterminate"
                         v-model="checkAll"
                         @change="handleCheckAllChange">Sellect All</el-checkbox>
          </div>

          <div class="price">Price</div>
          <div class="quantity">Quantity</div>
        </div>
        <div class="cart-main">
          <div class="product-item"
               v-for="(item,index) in cartList"
               :key="index">
            <el-checkbox v-model="item.checked"
                         @change="checkedChange(item.id)"></el-checkbox>
            <div class="product-detail">
              <div class="img">
                <img :src="item.img">
              </div>
              <div class="content">
                <div class="name">{{item.name}}</div>
                <div class="specification">Memory Capacity:4TB Color:special</div>
              </div>
            </div>
            <div class="product-price">
              ${{item.price}}
            </div>
            <div class="product-quantity">
              <el-input-number v-model="item.num"
                               size="small"
                               @change="qtyChange(item)"></el-input-number>
            </div>
          </div>
        </div>
        <div class="cart-subtotal">
          <div class="radio">
            <el-checkbox :indeterminate="isIndeterminate"
                         v-model="checkAll"
                         @change="handleCheckAllChange">Sellect All</el-checkbox>
          </div>
          <div class="total">
            Total : <span style="color:#e33333">${{totalPrice}}</span>
          </div>
          <div class="btn">
            Continue
          </div>
        </div>
      </div>

script

 data () {
    return {
      quantity: 4,
      checkAll: false,
      isIndeterminate: true,
      totalPrice: 0,
      cartList: [
        {
          id: 1,
          img: '//img10.joybuy.com/N4/jfs/t1/176101/8/13660/51184/60c01ea8E1204306a/1687f0f76f243965.jpg.dpg',
          name: 'Western Digital (WD) New My Passport 2.5 "Classic Black Mobile Hard Drive WDBYFT0020BBK-CESN',
          num: 1,
          price: 100,
          checked: true
        },
        {
          id: 2,
          img: '//img10.joybuy.com/N4/jfs/t1/176101/8/13660/51184/60c01ea8E1204306a/1687f0f76f243965.jpg.dpg',
          name: 'Western Digital (WD) New My Passport 2.5 "Classic Black Mobile Hard Drive WDBYFT0020BBK-CESN',
          num: 2,
          price: 200,
          checked: false
        },
        {
          id: 3,
          img: '//img10.joybuy.com/N4/jfs/t1/176101/8/13660/51184/60c01ea8E1204306a/1687f0f76f243965.jpg.dpg',
          name: 'Western Digital (WD) New My Passport 2.5 "Classic Black Mobile Hard Drive WDBYFT0020BBK-CESN',
          num: 3,
          price: 300,
          checked: true
        }
      ],
      cart: []
    }
  }
mounted () {
    this.setCart()
  },
  methods: {
    //全选
    handleCheckAllChange (val) {
      console.log(val)
      this.isIndeterminate = false;
      if (val == false) {
        for (let i in this.cartList) {
          this.cartList[i].checked = false
        }
      } else {
        for (let i in this.cartList) {
          this.cartList[i].checked = true
        }
      }
      this.setCart()
    },
    //数量改变
    qtyChange (value) {
      console.log(value)
      console.log(this.cartList)
      this.setCart()
    },
    //单选框改变
    checkedChange (id) {
      console.log(id)
      this.checkAll = this.cartList.every(function (obj) {
        return obj.checked == true
      })
      this.isIndeterminate = false
      this.setCart()
    },
    //购物车状态
    setCart () {
      let totalPrice = 0
      let allChecked = true
      this.cartList.forEach((v, i) => {
        if (v.checked) {
          totalPrice += v.num * v.price
        } else {
          allChecked = false
        }
      })
      this.allChecked = allChecked
      this.totalPrice = totalPrice
    }
  }

css

<style lang="scss" scoped>
.cart-group {
  margin-top: 30px;
  padding-bottom: 60px;

  .sellect-all {
    background: white;
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 20px;
  }

  .radio {
    width: 640px;
  }

  // .detail {
  //   width: 600px;
  // }

  .price {
    width: 220px;
  }

  .quantity {
    width: 200px;
  }

  .cart-main {
    padding: 20px;
    background: white;

    .product-item {
      display: flex;
      padding: 20px 0;
      border-bottom: 1px solid #e3e3e3;

      .product-detail {
        width: 550px;
        height: 100%;
        margin-left: 10px;
        padding: 0 40px 0 20px;
        display: flex;

        .img {
          width: 100px;
          height: 100px;

          img {
            width: 100px;
            height: 100px;
          }
        }

        .content {
          width: 450px;

          .name {
            font-size: 14px;
            color: black;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }

          .specification {
            font-size: 14px;
            color: #666;
            margin-top: 20px;
          }
        }
      }

      .product-price {
        width: 220px;
        height: 100%;
        font-size: 16px;
        color: #e33333;
      }

      .product-quantity {
        width: 220px;
        height: 100%;
      }
    }
  }

  .cart-subtotal {
    height: 60px;
    display: flex;
    align-items: center;
    margin-top: 30px;
    background: white;
    padding-left: 20px;
    position: relative;

    .btn {
      position: absolute;
      right: 0;
      padding: 0;
      width: 130px;
      height: 60px;
      font-size: 16px;
      text-align: center;
      line-height: 60px;
      color: #fff;
      background: #e33333;
    }

    .btn:hover {
      cursor: pointer;
    }
  }
}
</style>
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一些实现购物车模块的思路。首先,购物车模块需要记录用户加入购物车的商品信息,可以使用Vue中的响应式数据来实现。其次,可以使用Element UI中的表格组件来展示购物车中的商品信息,同时也可以添加删除商品等操作。下面是一个简单的实现步骤: 1. 创建一个Vue组件来表示购物车模块,可以命名为Cart.vue。 2. 在组件中使用data属性来定义响应式数据,可以定义一个数组来存储购物车中的商品信息,例如: ```javascript data() { return { cartItems: [] } } ``` 3. 在组件的模板中使用Element UI中的表格组件来展示购物车中的商品信息,例如: ```html <el-table :data="cartItems"> <el-table-column prop="name" label="商品名称"></el-table-column> <el-table-column prop="price" label="商品价格"></el-table-column> <el-table-column prop="quantity" label="商品数量"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="removeItem(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> ``` 4. 在组件中定义添加商品和删除商品的方法,例如: ```javascript methods: { addItem(item) { const index = this.cartItems.findIndex(cartItem => cartItem.id === item.id) if (index >= 0) { this.cartItems[index].quantity++ } else { this.cartItems.push({ id: item.id, name: item.name, price: item.price, quantity: 1 }) } }, removeItem(index) { this.cartItems.splice(index, 1) } } ``` 5. 在使用购物车模块的页面中,可以引入Cart组件并传递商品信息给它,例如: ```html <template> <div> <h1>商品列表</h1> <ul> <li v-for="item in items" :key="item.id"> {{ item.name }} - {{ item.price }} <el-button @click="addToCart(item)">加入购物车</el-button> </li> </ul> <h1>购物车</h1> <cart :cart-items="cartItems"></cart> </div> </template> <script> import Cart from '@/components/Cart.vue' export default { components: { Cart }, data() { return { items: [ { id: 1, name: '商品1', price: 100 }, { id: 2, name: '商品2', price: 200 }, { id: 3, name: '商品3', price: 300 } ], cartItems: [] } }, methods: { addToCart(item) { this.$refs.cart.addItem(item) } } } </script> ``` 这样就可以实现一个简单的购物车模块了。当用户点击“加入购物车”按钮时,会调用addToCart方法将商品信息添加到购物车中;购物车组件会响应数据的变化,自动更新表格中的内容。用户也可以在购物车中删除商品,或者修改商品数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值