VUE练习题---购物车


1.当全部商品被勾选时 全选选择框会自动勾选
2.购买数量可输入 通过长按可实现连续加减1
3.总价和购买数量随着选择自动发生变化

代码如下:

<template>
  <div>
      <table v-if="list.length">
          <thead>
              <th>全选<input type="checkbox" name="" id="" :checked="isAllchecked" @change="AllCheck"></th>
              <th>商品名称</th>
              <th>商品单价</th>
              <th>购买数量</th>
              <th>操作</th>
          </thead>
          <tr v-for="(value,index) in list" :key="index">
              <td><input type="checkbox" :checked='value.isChecked' @change="isCheck(index)"></td>
              <td>{{value.name}}</td>
              <td>{{value.price}}</td>
              <td>
                  <button @click="handleReduce(index)" @mousedown="longReduce(index)" @mouseup="stopReduce">-</button>
                  <input type="input" v-model="value.count" class="input"/>
                  <button @click="handleAdd(index)"  @mousedown="longAdd(index)" @mouseup="stopReduce">+</button>    
              </td>
              <td>
                  <button @click="handleRemove(index)">删除</button>
              </td>
          </tr>
          <tfoot>
              <th colspan="5">
                  购买数量:{{totalNum}}
                  总价为:¥{{totalPrice}}
             </th>
          </tfoot>
      </table>
      <div v-else>
          购物车为空
      </div>
  </div>
</template>

<script>
export default {
    name:'ShoppingCart',
    data(){
        return{
            timeOutEvent:'',
            isAllchecked:false,
            list:[
                {
                    name:'iPhone 7',
                    price:6188,
                    count:1,
                    isChecked:true
                },
                {
                    name:'三星999',
                    price:8765,
                    count:1,
                    isChecked:false
                },
                {
                    name:'华为Pro11',
                    price:3999,
                    count:1,
                    isChecked:false
                },
                {
                    name:'华为荣耀',
                    price:5599,
                    count:1,
                    isChecked:false
                }

            ]
        }
    },
    computed: {
        totalPrice:function(){
            let total=0;
            for(let item of this.list){
                if(item.isChecked===true){
                   total+=item.price*item.count
                }
            }
            return total
        },
        totalNum:function(){
            let total=0;
            for(let item of this.list){
                if(item.isChecked===true){
                    total+=item.count
                }
            }
            return total
        }
    },
    methods: {
        handleReduce(index){
            if(this.list[index].count>1){
            this.list[index].count--;
            }
        },
        handleAdd(index){
            this.list[index].count++;
        },
        handleRemove(index){
            this.list.splice(index,1)
        },
        AllCheck(){
            this.isAllchecked=!this.isAllchecked;
            if(this.isAllchecked===true){
                for(let item of this.list){
                    item.isChecked=true
                }
            }else{
                for(let item of this.list){
                    item.isChecked=false
                }
            }
        },
        isCheck(index){
            this.list[index].isChecked=!this.list[index].isChecked;
            let flag=true;
            for(let item of this.list){
                if(item.isChecked===false){
                    flag=false
                }
            }
            this.isAllchecked=flag
        },
        // 长按加一
        longAdd(index){
            this.timeOutEvent=setInterval(() => {
                this.list[index].count++;
            }, 300);
        },
        longReduce(index){
            this.timeOutEvent=setInterval(() => {
                if(this.list[index].count>1)
                this.list[index].count--
            }, 300);
        },
        stopReduce(){
            clearInterval(this.timeOutEvent)
        }
    },
    

}
</script>

<style>
table{
    width: 100%;
    text-align: center;
    border-collapse: collapse;
}
table,th,td{
    border: 1px solid black;
}
th{
    background-color: rgb(126, 150, 56);
    height: 50px;
}
tr{
    height: 50px;
}
tr:nth-child(odd){     /*双数变色用even*/
    background-color: #EAF2D3;
    }
.input{
    width: 20px;
    text-align: center;
}
/* tr:nth-child(even){background-color:  #EAF2D3}    */
</style>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种流行的JavaScript框架,用于构建用户界面。它采用了组件化的开发方式,使得开发者可以将页面拆分成多个独立的组件,每个组件负责自己的逻辑和样式。Vue具有简洁易学的语法和高效的性能,因此在前端开发中得到了广泛应用。 Vue-grid-layout是Vue的一个插件,用于实现网格布局。它提供了一个可拖拽和可调整大小的网格系统,使得开发者可以轻松地创建灵活的布局。通过使用vue-grid-layout,开发者可以将页面元素按照网格的形式进行排列,并且可以通过拖拽和调整大小来改变元素的位置和尺寸。 以下是一个简单的示例代码,展示了如何在Vue使用vue-grid-layout: ```html <template> <div> <vue-grid-layout :layout="layout" :col-num="12" :row-height="30" :is-draggable="true" :is-resizable="true"> <div v-for="item in layout" :key="item.i" :data-grid="item"> {{ item.i }} </div> </vue-grid-layout> </div> </template> <script> import VueGridLayout from 'vue-grid-layout'; export default { components: { VueGridLayout }, data() { return { layout: [ { x: 0, y: 0, w: 2, h: 2, i: '1' }, { x: 2, y: 0, w: 4, h: 2, i: '2' }, { x: 6, y: 0, w: 2, h: 4, i: '3' } ] }; } }; </script> ``` 在上面的代码中,我们首先导入了vue-grid-layout插件,并将其注册为Vue组件。然后,在模板中使用`vue-grid-layout`标签来创建一个网格布局容器。通过设置不同的属性,我们可以定义网格的列数、行高、是否可拖拽和调整大小等。在`vue-grid-layout`标签内部,我们使用`v-for`指令来遍历`layout`数组,并将每个元素渲染为一个网格项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值