zoukankan      html  css  js  c++  java
  • vuejs实现购物车功能

    <body>
    <h1 align="center">购物车实例</h1>
    <div id="vm1">
    <table align="center" width="800" border="1" cellpadding="0" cellspacing="0" >
    <tr>
    <th>序号</th>
    <th>商品</th>
    <th>单价</th>
    <th>数量</th>
    <th>小计</th>
    <th>操作</th>
    </tr>
    <tr v-for="(product,index) in productsList">
    <td>{{Number(index)+1}}</td>
    <td>{{product.name}}</td>
    <td class="money">{{product.price}}</td>

    <td>
    <button v-on:click="jian(index)">-</button>

    <input type="text" v-model="product.num" v-on:input="change(index)">

    <button v-on:click="add(index)">+</button>
    </td>
    <td class="money">{{product.price*product.num}}</td>
    <td><button v-on:click="del(index)">移除购物车</button></td>
    </tr>
    </table>

    <table align="center" width="800" border="1" cellpadding="5" cellspacing="0" style="margin-top: 60px;">
    <tr>
    <th>购买商品总数量</th>
    <th>总价</th>
    </tr>
    <tr>
    <td>{{ count2() }}</td>
    <td class="money">{{ allMoney() }}</td>

    </tr>
    </table>
    </div>

    <script>
    var vm1=new Vue({
    el:'#vm1',
    data:{
    productsList:[
    {id:'0',name:'iPhone 6 plus',price:'1000',num:1},
    {id:'1',name:'电脑桌',price:'300',num:1},
    {id:'2',name:'平板',price:'800',num:1}
    ]
    },
    methods:{
    // 删除
    del:function(index){
    if(confirm("您确定要删除吗?")){
    this.productsList.splice(index,1);
    }
    },
    // 增加
    add:function(index){
    if(this.productsList[index].num<100){
    this.productsList[index].num++;
    }
    },
    // 减少
    jian:function(index){
    if(this.productsList[index].num>1){
    this.productsList[index].num--;
    }
    },
    // 修改
    change:function(index){
    if(this.productsList[index].num>100){
    this.productsList[index].num=100;
    }else if(this.productsList[index].num<1){
    this.productsList[index].num=1;
    }
    },
    // 总数量
    count2:function(){
    var List=this.productsList;
    var total=0;
    for(var i=0;i<List.length;i++){
    total+=parseInt(List[i].num);
    }
    return total;
    },

    // 总金额
    allMoney:function(){
    var List=this.productsList;
    var totalMoney=0;
    for(var i=0;i<List.length;i++){
    totalMoney+=parseInt(List[i].num)*List[i].price;
    }
    return totalMoney;
    }


    }
    });


    </script>
    </body>

  • 相关阅读:
    使非标准 Win32 控件或自画控件也具有 Windows XP 的界面风格
    MapInfo格式到ArcInfo格式的转换
    DICOM医学图像文件格式
    香港身份证
    Cheap Tricks: Let's Talk About METADATA TypeLibs
    ASP中使用ADO访问数据源
    DirectX 9 编程 DirectX窗口
    3DES Source Code
    OLEDB Resource(Session) Pooling (在Ado开发中使用连接池)
    《仙剑奇侠传4》仙剑问答全答案
  • 原文地址:https://www.cnblogs.com/yanyufusu/p/7875279.html
Copyright © 2011-2022 走看看