Vue实现简单购物车效果

效果:总计会随着总价的变化而变化,总价随着数量的变化而变化


QQ截图20180914094046.png

body部分:

<div id="app">
   <table border='1' cellspacing='0'>
       <thead>
           <tr>
               <th>编号</th>
               <th>名称</th>
               <th>单价</th>
               <th>数量</th>
               <th>总价</th>
           </tr>
       </thead>
       <tbody>
           <tr v-for='(value,index) in arr'>
               <td>{{value.num}}</td>
               <td>{{value.uname}}</td>
               <td>{{value.price}}</td>
               <td>
                   <button v-on:click='jia(index)'>+</button>
                       {{value.quantity}}
                   <button v-on:click='jian(index)'>-</button>
               </td>
               <td>{{value.total}}</td>
           </tr>
           <tr>
               <td colspan='5' style='text-align:center;'>总计:<span>{{arrs}}</span>元</td>
           </tr>
       </tbody>
   </table>
</div>

js部分:

new Vue({
        el:'#app',
        data:{
            arr:[
                {num:1,uname:'香蕉',price:1,quantity:1,total:1},
                {num:2,uname:'苹果',price:2,quantity:1,total:2},
                {num:3,uname:'橘子',price:3,quantity:1,total:3}
            ],
            arrs:6
        },
        methods:{
            jia:function(ind){
                this.arr[ind].quantity++
                this.arr[ind].total=(this.arr[ind].price)*this.arr[ind].quantity
                this.getTotal()
            },
            jian:function(index){
                if(this.arr[index].quantity>1){
                    this.arr[index].quantity--
                    this.arr[index].total=this.arr[index].price*this.arr[index].quantity
                }
                this.getTotal()
            },
            getTotal:function(){
                for(var i=0,to=0;i<this.arr.length;i++){
                    to+=Number(this.arr[i].total)
                }
                this.arrs=to;
            }
        }
    })

style部分:

<style>
    th,td{
        border:1px solid #000;
        width:100px;
        height:30px;
        text-align: center;
    }
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,019评论 3 119
  • 四月 红了樱桃 绿了芭蕉 雨 是过客 十月 黄了深秋 白了寒冬 风 是过客 此生 笑了春夏 苦了半生 亦是 过客
    jscfc阅读 129评论 1 6
  • 死水微澜,一天的行情四个字足矣。又浪费掉一天生命,活受罪。说实话,老荀迷迷糊糊睡到收盘——还没醒呢!有诗为证: ...
    谈天看地阅读 284评论 0 0
  • 高考完的出游是必定的安排,磨磨蹭蹭78月的时候才出发,因为是去欧洲语言不通环境不受,所以小姨选择了跟团游。这必然是...
    小熊饼干被注册了阅读 618评论 0 1
  • 这绝对是上海设计师设计,好大的地下空间,人来人往,川流不息,好多卖高价饭的大店面,可洗手间只有三蹲位,三个槽,感觉...
    阿钢锅阅读 987评论 0 0