vue+vant+js实现购物车原理小demo(中级版有选择)

摘要:
添加仅计算所选购物车项目的功能。

增加只计算已选的的购物车商品功能。效果图:

vue+vant+js实现购物车原理小demo(中级版有选择)第1张

 main.js:

Vue.use(Stepper);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);

 上代码:

<template>
  <div class="box">
    <div class="flex-left tit">
      <span v-for="(i,inx) in tit" :key="inx" class="flex-around">{{i}}</span>
    </div>
    <div v-for="(item,s) in arr" :key="s" class="flex-around">
      <van-checkbox v-model="item.checked" @change='changeBox(item.checked,s)'></van-checkbox>
      <span>{{item.id}}</span>
      <span>{{item.name}}</span>
      <span>{{item.price}}</span>
      <van-stepper v-model="item.value" @change='change(item.value,s)'/>
      <input type="text" v-model='item.smallCount'>元
    </div>
    <p>共计:一共{{allValue}}件,共<input type="text" v-model="allCount">元</p>
  </div>
</template>
<script>
export default {
    name: "push",
    data(){
        return{
           tit:['序号','名称','单价','数量','小计'],
           arr:[],             //购物车
           allCount:0,         //价格总计
           allValue:0          //数量总计
        }
    },
    components:{
    },
    created:function(){
      this.jsfun()
    },
    methods:{
      jsfun(){
        let arr = []
        let obj1={
          id:1,
          name:'足球',
          price:10,
          value:1,
          checked:true
        }
        let obj2={
          id:2,
          name:'篮球',
          price:20,
          value:1,
          checked:true
        }
        let obj3={
          id:3,
          name:'水球',
          price:50,
          value:1,
          checked:true
        }
        arr.push(obj1)
        arr.push(obj2)
        arr.push(obj3)
        arr.forEach(element => {
          element.smallCount = element.price*element.value
          this.allCount += element.price
        });
        console.log(arr)
        this.arr = arr                            //先在页面加载时生成两条购物车数据
        this.allValue = this.arr.length           //先在页面加载时生成购物车内商品数量 
      },
      change(value,s){
         console.log(value,s)                     //value是当前购物车已选择的值,s是当前购物车下标
         let arr = this.arr
         let allCount = 0
         let allValue = 0
         for(let i=0;i<arr.length;i++){
           arr[s].smallCount = arr[s].price*arr[s].value
           if(arr[i].checked){
              allCount += arr[i].smallCount
              allValue += arr[i].value
            }
         }
         this.allCount = allCount
         this.allValue = allValue
      },
      changeBox(val,s){
        console.log(val,s)
        let arr = this.arr
        let allCount = 0
        let allValue = 0
        for(let i=0;i<arr.length;i++){
          if(arr[i].checked){
            allValue+= arr[i].value
            allCount+= arr[i].smallCount
          }
          this.allValue = allValue
          this.allCount = allCount
        }
      }
    } 
}
</script>
<style lang="less" scoped>
.box{
  font-size: 12px;
  background: white;
  padding: 10px 0;
  p{
   margin-top: 10px;
  }
}
.tit{
  margin-bottom: 10px;
  :nth-child(1){
     7%;
  }
  :nth-child(2){
     7%;
  }
  :nth-child(3){
     9%;
  }
  :nth-child(4){
     27%;
  }
  :nth-child(5){
     44%;
  }
}
.van-stepper {
    font-size: 0;
    user-select: none;
    display: flex;
}
</style>

免责声明:文章转载自《vue+vant+js实现购物车原理小demo(中级版有选择)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇application.properties文件配置VC6.0 +WDK 开发驱动的环境配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Fiddler抓包7-post请求(json)(转载)

2.查看上图中的红色框:这里只支持application/x-www-form-urlencoded格式的body参数,即json格式。您需要检查JOSN列中的five和xml。1.如果遇到text/xml格式的正文,如下图所示...

uniapp 实现动态切换全局主题色

要求:要在开发的应用程序中切换主题颜色,如果只需要一种主题颜色,但不需要切换,则可以使用uniappSCSS文件文档思想:预先在公共css中定义所需的主题颜色。这里只是一个定义两种颜色的参考文档的示例,可以从中获得想法。您可以使用css属性选择器动态设置数据xx以动态更改主题颜色。最初,您希望将一个变量直接混合到mixin中,以实现主题颜色的全局控制,忽略了...

Navicat数据存放位置和备份数据库路径设置

navicat数据库存储在哪里?有了这样的问题,让我们来解决这个问题。默认情况下安装Navicat,默认情况下也安装MySQL,数据库存储在默认用户的目录中。选择安装目录时,还可以选择数据的位置。很多人此时只是设置了MySQL的安装位置。...

matlab中figure 创建图窗窗口

示例figure将f指定的图窗作为当前图窗,并将其显示在其他所有图窗的上面。figure;同时使用多个图窗创建两个图窗,然后创建一个线图。f1=figure;f2=figure;plot;将当前图窗设置为f1,使其成为下一个绘图的目标。figure;scatter;输入参数全部折叠f-目标图窗Figure对象目标图窗,指定为Figure对象。默认情况下,Nu...

Qt HWND的句柄与QWidget的转换

QT中使用hwnd的句柄在编程中遇到问题。第三方API使用HWND类型作为正式参数,但该类型在QT中不可用。您可以执行以下操作来解决问题。首先声明:HWNDm in。h-hWnd;然后声明1public:2QWidget*newWidget;在In-cpp中,newWidget=QWidget::find//您需要将QWidget转换为HWND1QWidge...

pageoffice

--PageOfficeEnd--˃4.这将更改为印章简单管理页面的管理员登录密码。为了安全起见,强烈建议更改它!!!如果您使用Oracle、MySQL和其他数据库,请删除以下节点上的注释,并将其修改为密封数据库连接参数。最好删除印章。WEB-INF库下的mdb。...