vue学习笔记

vue学习第一天笔记

2019-01-03  本文已影响0人  果木山

VUE

购物车实例

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>购物车实例</title>
     <link rel="stylesheet" href="css/bootstrap.css">
     <style>
         .table-bordered{
             border: 2px solid green;
         }
         .table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr > td{
             border: 1px solid red;
             vertical-align: middle;
         }
         .btn-danger{
             border-width: 0;
             padding: 5px 15px;
         }
     </style>
 </head>
 <body>
 <div class="container" id="app" style="margin-bottom: 100px;">
     <div class="row">
         <div class="col-md-12">
             <table class="table table-bordered" style="font-size: 20px;">
                 <caption class="text-danger" style="font-size:30px;font-weight: 700">购物车</caption>
                 <tr>
                     <th class="text-center">商品名称</th>
                     <th class="text-center">商品价格</th>
                     <th class="text-center">操作</th>
                 </tr>
                 <tr v-for="(item,index) in ary">
                     <td class="text-center">{{item.name}}</td>
                     <td class="text-center">{{item.price}}</td>
                     <td class="text-center"><button type="button" class="btn-danger" @click="deleteOne(index)">删除</button></td>
                 </tr>
                 <tr v-if="ary.length?true:false">
                     <td colspan="3" class="text-right"><button type="button" class="btn-danger" @click="deleteAll">删除全部</button></td>
                 </tr>
             </table>
         </div>
         <div class="col-md-12">
             <form style="font-size: 20px;">
                 <div class="form-group">
                     <label for="shopname">商品名称</label>
                     <input type="text" class="form-control" id="shopname" v-model="shopname">
                 </div>
                 <div class="form-group">
                     <label for="shopprice">商品价格</label>
                     <input type="number" class="form-control" id="shopprice" v-model="shopprice">
                 </div>
                 <div class="from-group">
                     <input type="button" id="dd" class="btn btn-primary" @click="addOne" value="添加">
                     <input type="button" id="dd1" class="btn btn-warning" @click="resetData" value="重置">
                 </div>
             </form>
         </div>
     </div>
 </div>
 <script src="js/vue.js"></script>
 <script>
     var app=new Vue({
         el:"#app",
         data:{
             shopname:"",
             shopprice:"",
             ary:[
                 {name:"小米6",price:1999},
                 {name:"小米7",price:2999},
                 {name:"小米8",price:3999},
                 {name:"小米9",price:4999}
             ]
         },
         methods:{
             deleteOne:function (index) {
                 console.log(this)
                 this.ary.splice(index,1);
             },
             deleteAll:function () {
                 this.ary=[];
             },
             addOne:function () {
                 //表单中输入内容不为空时,才能添加;
                 if(this.shopname && this.shopprice){
                     //如果商品名称重复,则改变其价格,阻止程序执行;
                     for(var i=0; i<this.ary.length; i++){
                         if(this.ary[i].name===this.shopname){
                             this.ary[i].price=this.shopprice;
                             this.shopname="";
                             this.shopprice="";
                             return;
                         }
                     }
                     //当商品名称不重复时,才能改变价格
                     this.ary.push({
                         name:this.shopname,
                         price:this.shopprice
                     });
                     //添加后,清空表单中内容;
                     this.shopname="";
                     this.shopprice="";
                 }else{
                     alert("内容不能为空,请输入内容")
                 }
             },
             resetData:function () {
                 this.shopname="";
                 this.shopprice="";
             }
         }
     })
 </script>
 </body>
 </html>

简书知识

vue学习大纲1-基础,事件和数据交互

上一篇 下一篇

猜你喜欢

热点阅读