vue 购物车案例(加全选,单选)

本文介绍了如何在Vue项目中实现购物车功能,包括全选和单选操作。特别提示,由于使用了特定的JSON数据格式,直接操作代码可能遇到问题,建议在Visual Studio Code中进行编译并利用其格式化功能。
摘要由CSDN通过智能技术生成

vue 购物车案例(加全选,单选)

<template> 
 <div class="home">  
 //左右联动的左部分
   <div class="left">     
    <div      
      :id="selindex===index? 'sel':''"       
       v-for="(item,index) in data"        
       :key="index"       
        @click="onlick(index)"      >
        {
   {
   item.anchor_point}}
        </div>  
          </div>
    <div class="right" ref="res"> 
   //左右联动的右侧
         <div ref="qq" style="width:100%;"
          v-for="(item,index) in data" :key="index">
   <div v-for="(items,index) in item.commodity_list" :key="index">
       <div class="div">      
           <img :src="items.comm_image" width="50px" height="50px" /> 
       <div style="width:100%">              
       <p style="font-size:12px;">{
   {
   items.comm_title}}</p>              
       <p class="po" style="font-size:12px;color:red;margin-top:10px;">
       <span>{
   {
   items.fabulous}}</span>                
       <span class="iconfont icon-jiahao" @click="toCart(items)"></span>              
       </p>   
      </div>         
       </div>       
        </div>    
          </div>  
            </div>   
             <div class="bottom">     
             //购物车的内容
                <p class="cc">            
                <span class="iconfont icon-gwc" @click="th"></span>           
                 <span class="math">{
   {
   this.cart.length}}</span>     
                    </p>     
                       <p style="color:red"> 
                       <span>{
   {
   totalMoney}}</span> &nbsp;&nbsp;
                       <span>{
   {
   nn}}</span> 
                        </p>        
                        <p style="width:110px;text-align:center;background:red;">结算</p>   
                         </div>   
                          <div class="d" v-show="show">       
                           <p >            
                            <span @click="ch">x</span> &nbsp;        
                                <span>               
                                  <input type="checkbox" v-model="one"
                                   @click="change" />全选            
                              </span>            
                                       </p>    
    <div class="sd" v-for="(item,index) in cart" :key="index">    
             <input type="checkbox" class="c" :checked="item.checked" @click="OnClick(item,index)" />       
                 <img :src="item.dd.comm_image" width="50px" height="50px" />            <div style="width:100%">          
                     <p style="font-size:12px;">{
   {
   item.dd.comm_title}}</p>   
                    <p class="po" style="font-size:12px;color:red;margin-top:10px;">
                    <span>{
   {
   item.dd.fabulous}}</span>                
                    <span style="margin-right:15px;">             
                     <button @click="jian(item)">-</button>            
                       {
   {
   item.num}}              
                       <button @click="jia(item)">+</button>           
                        </span>              
           <span @click="del(index)" style="float:right" class="iconfont icon-shanchu">
           </span>             
            </p>            
            </div>        
            </div>    
            </div>  
            </div>
            </template>
<script>
export default {
   
        data() {
   
            return {
      
               data: [],   
              selindex: 0, 
              show: false,  
              cart:[],   
              one:false   
               }; 
                },  
       computed: {
       
           del(index) {
   
可以参考以下代码实现购物车单选全选功能: ``` <template> <div> <div v-for="(item, index) in cartList" :key="index"> <label> <input type="checkbox" v-model="item.checked" @change="handleCheckboxChange"> {{ item.name }} </label> <span>{{ item.price }}</span> </div> <div> <label> <input type="checkbox" v-model="isAllChecked" @change="handleAllCheckboxChange"> 全选 </label> <span>{{ totalPrice }}</span> </div> </div> </template> <script> export default { data() { return { cartList: [ { name: '商品1', price: 10, checked: false }, { name: '商品2', price: 20, checked: false }, { name: '商品3', price: 30, checked: false } ] } }, computed: { isAllChecked() { return this.cartList.every(item => item.checked) }, totalPrice() { return this.cartList.filter(item => item.checked).reduce((total, item) => total + item.price, 0) } }, methods: { handleCheckboxChange() { this.$nextTick(() => { this.isAllChecked = this.cartList.every(item => item.checked) }) }, handleAllCheckboxChange() { this.cartList.forEach(item => { item.checked = this.isAllChecked }) } } } </script> ``` 在上面的代码中,我们首先定义了一个 `cartList` 数组来存储购物车中的商品信息,每个商品包括名称、价格和是否选中三个属性。接着,我们通过 `v-for` 指令遍历 `cartList` 数组,动态生成购物车列表。 在购物车列表中,我们使用了一个复选框来表示每个商品的选中状态,使用了 `v-model` 指令来双向绑定商品的 `checked` 属性。当用户点击某个商品的复选框时,我们通过 `@change` 监听其变化,并在 `handleCheckboxChange` 方法中更新全选复选框的选中状态。 在最后一行,我们使用了一个全选复选框来表示购物车中所有商品的选中状态。与单个商品的复选框类似,我们使用了 `v-model` 指令来双向绑定全选复选框的 `isAllChecked` 属性。当用户点击全选复选框时,我们通过 `@change` 监听其变化,并在 `handleAllCheckboxChange` 方法中更新所有商品的选中状态。同时,我们还使用了计算属性来计算购物车中所有选中商品的总价。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值