Vue常用指令的巩固练习

一、模拟购物车

如下图所示,结合bootstrap(bootstrap中文网地址:http://www.bootcss.com/)框架用现在所学的指令完成
要求:点击加或减按钮时数量、加减、总计也随之加减

模拟购物车.png

代码如下:
方法一:

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
  <div class="container">
    <table class='table table-bordered text-center'>
      <thead>
        <tr>
          <th>编号</th>
          <th>名称</th>
          <th>单价</th>
          <th>数量</th>
          <th>总价</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(value,index) in arr">
          <td>{{index+1}}</td>
          <td>{{value.name}}</td>
          <td>{{value.price}}</td>
          <td>
            <button v-on:click="jian(index)"></button>
            {{value.num}}
            <button v-on:click="add(index)"></button>
          </td>
          <td>{{value.price*value.num}}</td>
        </tr>
        <tr>
          <td colspan="5">总计:{{sum}}</td>// colspan="5":合并单元格=“合并5列”
        </tr>
      </tbody>
    </table>
  </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<script>
new Vue({
  el:".container",
  data:{
    arr:[
      {name:"香蕉",price:1,num:0}
      {name:"苹果",price:2,num:0}
      {name:"鸭梨",price:3,num:0}
    ],
    sum:0
  },
  methods:{
    jian:function(ind){
      if(this.arr[ind].num=0){
        this.num=0
      }else{
        //num数量减
        this.arr[ind].num--
        //总计
        this.sum-=this.arr[ind].price
      }
    } ,
    add:function(ind){
      //num数量减
      this.arr[ind].num++  
      //总计
      this.sum+=this.arr[ind].price
    }
  }
})
</script>

方法二:

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
    <div class='container'>
        <table class='table table-bordered text-center'>
            <thead>
                <tr>
                    <th>编号</th>
                    <th>品名</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(value,index) in list">
                    <td>{{index+1}}</td>
                    <td>{{value.pname}}</td>
                    <td>{{value.price}}</td>
                    <td>
                        <button @click='add(index)'>+</button>
                        <span>{{value.count}}</span> 
                         <button @click='redu(index)'>-</button>
                     </td>
                    <td>{{value.sub}}</td>
                </tr>
                <tr>
                    <td colspan="5">总价:¥{{sum}}</td>
                </tr>
            </tbody>
        </table>
    </div>
  <script src='../js/vue.js'></script>
  <script type="text/javascript">
    new Vue({
        el:'.container',
        data:{
            list:[
                 {pname:'香蕉',price:1,count:2,sub:2},
                 {pname:'苹果',price:2,count:3,sub:6},
                 {pname:'鸭梨',price:3,count:4,sub:12}
            ],
            sum:0
        },
        methods:{
            add:function(ind){
                //数量
                this.list[ind].count++;
                //改变小计
                this.list[ind].sub=this.list[ind].count*this.list[ind].price;
                this.total();
            },
            redu:function(ind){
                //数量
                if(this.list[ind].count>1){
                   this.list[ind].count--;
                }
                //小计
                this.list[ind].sub=this.list[ind].count*this.list[ind].price;
                  this.total();
            },
            total:function(){
                for(var i=0,tota=0;i<this.list.length;i++){
                   tota+=this.list[i].sub
                }
                this.sum=tota
            }
        }
    })
  </script>
</body
二、模拟用户录入

结合bootstrap(bootstrap中文网地址:http://www.bootcss.com/)框架完成
要求:点击添加按钮可以把输入框中的内容添加到table中,点击重置按钮可把当前出入框中的内容清空,点击每行的删除也可以当前行删除掉

模拟用户录入

代码如下:

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
</head>
<div class="container">
    <form action="">
        <div class='form-group'>
            <label for="exampleInputEmail1">姓名</label>
            <input type="text" class="form-control" placeholder="请输入您的姓名" v-model="arrs.name">
        </div>
        <div class='form-group'>
            <label for="exampleInputEmail1">年龄</label>
            <input type="text" class="form-control" placeholder="请输入您的年龄" v-model="arrs.old">
        </div>
        <div class='form-group'>
            <label for="exampleInputEmail1">邮箱</label>
            <input type="text" class="form-control" placeholder="请输入您的邮箱" v-model="arrs.mail">
        </div>
        <div class='form-group text-center'>
            <input type="text" class='btn btn-success' value='添加' v-on:click="add">
            <input type="text" class='btn btn-info' value='重置' v-on:click="chong">
        </div>
    </form>
    <table class='table table-bordered'>
        <thead>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>邮箱</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(value,index) in arr">
                <td>{{index+1}}</td>
                <td>{{value.name}}</td>
                <td>{{value.old}}</td>
                <td>{{value.mail}}</td>
                <td><button v-on:click="clear">删除</button></td>
            </tr>
        </tbody>
    </table>
</div>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el:'.container',
        data:{
            arr:[
                {name:'Tom',old:'18',mail:'Tom@126.com'},
                {name:'Kimi',old:'18',mail:'Kimi@126.com'},
                {name:'Mark',old:'18',mail:'Mark@126.com'}
            ],
            arrs: {}

        },
        methods:{
            add:function(){
                //方法一
                /*this.arr.push(this.arrs)
                this.arrs={}*/
                //浅克隆  方法二
                var newarrs={}
                for(var i in this.arrs){
                    newarrs[i]=this.arrs[i]
                }
                this.arr.push(newarrs)
                this.arrs={}

            },
            chong:function(){
                this.arrs={}
            },
            clear:function(index){
                this.arr.splice(index,1)
            }

        }
    })
</script>
三、选项卡

要求:运用v-show显示或隐藏来完成


Image 4.png

代码如下:

<style>
    li{
        list-style: none;

    }
    .name{
        display: inline-block;
        border: 2px solid saddlebrown;
        width: 100px;
        line-height: 25px;
        text-align: center;
    }
    .names{
        border: 2px solid saddlebrown;
        width: 308px;
        margin-top: -16px;
        margin-left: 40px;
        text-align: center;
        line-height: 60px;
    }
</style>
<div id="itany">
    <ul>
        <li class="name" v-for="(value,index) in arr" @click="fun(index)">{{value.text}}</li>
    </ul>
    <p class="names clearfix" v-for="(value,index) in arr" v-show="value.see">{{value.title}}</p>
</div>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el:"#itany",
        data:{
            arr:[
                {text:'选项一',title:'我是选项一',see:true},
                {text:'选项二',title:'我是选项二',see:false},
                {text:'选项三',title:'我是选项三',see:false}
            ]
        },
        methods:{
            fun:function(ind){
                for(var i= 0;i<this.arr.length;i++){
                    this.arr[i].see=false
                }
                this.arr[ind].see=true
            }
        }
    })
</script>

多加练习,不荒废现在的时间,对得起未来的自己!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容

  • jquery介绍 jQuery是目前使用最广泛的javascript函数库 据统计,全世界排名前100万的网站,有...
    就是这么帅_567e阅读 1,051评论 0 0
  • css3渐变等相关特效【菜鸟教程】 http://www.runoob.com/css3/css3-gradien...
    媛蜜儿阅读 1,854评论 0 0
  • 一、 移动端js事件 移动端的操作方式和PC端是不同的,移动端主要用手指操作,所以有特殊的touch事件,touc...
    小袋鼠cf阅读 212评论 0 0
  • 移动端js事件 移动端的操作方式和PC端是不同的,移动端主要用手指操作,所以有特殊的touch事件,touch事件...
    心软脾气硬01阅读 685评论 0 1
  • 移动端js事件 移动端的操作方式和PC端是不同的,移动端主要用手指操作,所以有特殊的touch事件,touch事件...
    jxvl假装阅读 1,019评论 0 0