Vue实现简单的购物车

本文介绍了如何利用Vue.js框架来实现一个简单的购物车功能。通过结合组件化思想和Vue的响应式特性,实现了商品的增删及数量调整,帮助读者理解Vue在实际电商场景中的应用。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="vue.min.js"></script>
             <link rel="stylesheet" type="text/css" href="bootstrap.min.css">  
  <script src="jquery.min.js"></script>
  <script src="bootstrap.min.js"></script>
</head>
<body>
  <div id="app">
            <table class="table table-bordered" id="productListTable" >
                <thead>
                    <tr>
                    <th>商品名称</th>
                    <th>单价</th>
                    <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="product in products ">
                        <td>{{product.name}}</td>
                        <td>¥{{product.names}}</td>

             <td>
                            <a href="#nowhere" @click="edit(product)">编辑</a>
                            <a href="#nowhere" @click="deleteproduct(product.id)">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            商品名称:
                            <input type="text"  class="form-control"  v-model="productAdd.name" />
                            <br>
                            单价:
                            <input type="number"   class="form-control"   v-model="productAdd.names" />
                            <br>
                            <button type="button" class="btn btn-default"  v-on:click="add">增加</button> 
                         </td>                  
                    </tr>
                </tbody>
            </table>

            <div id="Update">
          <table class="table table-bordered">   
          <tr>
    <td>
商品名称:
                            <input type="text"  class="form-control"   v-model="productUpdate.name" />
    </td>
          </tr> 

          <tr>
    <td>
单价:
                            <input type="number" class="form-control"    v-model="productUpdate.names" />
    </td>
          </tr>
              <tr>
    <td>
 <input type="hidden"    v-model="productUpdate.id" />
    </td>
          </tr>
    <tr>
    <td>
 <button type="button" class="btn btn-default"  v-on:click="update">修改</button>                
                            <button type="button" class="btn btn-default"  v-on:click="cancel">取消</button>   
    </td>
          </tr>
     </table>         
            </div>
    </div>
     <script type="text/javascript">
        //修改区域隐藏起来先
        $("#Update").hide();

        //Model
        var data = {
            products: [
            { id1name'红米4A',names799},
            { id2name'红米5A'names899},
            { id3name'红米6A'names999},
            { id4name'红米20k'names2399},
            { id5name'小米9'names3299},
            ],
            productAdd: { id0name''names'0'},
            productUpdate: { id0name''names'0'}
        };

        //用于记录最大id值
        var maxId = 5;
        //计算最大值
        for (var i=0;i<data.products.length;i++){
            if (data.products[i].id > maxId)
                maxId=  this.products[i].id;
        }      

    //ViewModel
    var vue = new Vue({
        el'#app',
        data: data,
        methods: {
            addfunction (event{
                //获取最大id
                maxId++;
                //赋予新id
                this.productAdd.id = maxId;
                if(this.productAdd.name.length==0)
                    this.productAdd.name = "Product"+this.productAdd.id;
                //把对象加入到数组
                this.products.push(this.productAdd);
                //让 productAdd 指向新的对象
                this.productAdd = { id0name''names'0'}
            },
            deleteproductfunction (id{
                console.log("id"+id);
                for (var i=0;i<this.products.length;i++){
                    if (this.products[i].id == id) {
                        this.products.splice(i, 1);
                        break;
                    }
                }
            },
            editfunction (product{
                $("#productListTable").hide();
                $("#Update").show();
                this.productUpdate = product;
            },
            update:function(){
                //因为v-model,已经同步修改了,所以只需要进行恢复显示就行了
                $("#productListTable").show();
                $("#Update").hide();          
            },
            cancel:function(){
                //其实就是恢复显示
                $("#productListTable").show();
                $("#Update").hide();
            }
        }
    });
    
</script>
</body>
</html>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190626000513395.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwODYxNTYx,size_16,color_FFFFFF,t_70)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值