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>

  • 相关阅读:
    Linux下压缩文件和解压缩
    原生JS随机数
    Git问题-Git warning LF will be replaced by CRLF
    mysql进程占用cpu居高不下处理
    mysql修改查询的结果包含的敏感字
    mysql 全库备份和还原
    mysql创建用户并设置权限
    Mysql-开启 SSL加密 mysql_ssl_rsa_setup
    tp6_005控制器
    ERROR 1104 (42000): The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
  • 原文地址:https://www.cnblogs.com/yanyufusu/p/7875279.html
Copyright © 2011-2022 走看看