VUE简易购物车程序

目录

效果预览图

完整代码


效果预览图

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        table{
            width: 700px;
            border-collapse: collapse;
            margin: 50px auto;
        }
        tr{
            height: 50px;
        }
        th{
            background: #eee;
        }
        td{
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="app">
        <table border="1">
            <tr>
                <th><input type="checkbox" v-model="qx" @change="aa" ></th>
                <th>序号</th>
                <th>书名</th>
                <th>单价</th>
                <th>数量</th>
                <th>总价</th>
                <th>操作</th>
            </tr>
            <tr v-for="(item,index) in arr">
                <td><input type="checkbox" v-model="item.bol" @change="qx2"></td>
                <td>{{index}}</td>
                <td>{{arr[index].name}}</td>
                <td>{{arr[index].dj}}</td>
                <td><button @click="zj(index)">+</button>{{arr[index].num}}<button @click="js(index)">-</button></td>
                <td>{{arr[index].dj*arr[index].num}}</td>
                <td><button @click="sc(index)">删除</button></td>
            </tr>
            <tr>
                <td>总价</td>
                <td colspan="6">{{total}}</td>
            </tr>
        </table>
    </div>
</body>
</html>
<script type="module">
import {createApp} from './js/vue.esm-browser.js';
createApp({
    data() {
        return {
            arr:[
                {name:'我在精神病院学斩神',dj:'39',num:1, bol:true},
                {name:'诛仙',dj:'59',num:1, bol:true},
                {name:'剑来',dj:'39',num:1, bol:true},
                {name:'雪中悍刀行',dj:'49',num:1, bol:true},
                {name:'大奉打更人',dj:'59',num:1, bol:true},
                {name:'完美世界',dj:'49',num:1, bol:true},
                {name:'仙逆',dj:'29',num:1, bol:true},
            ],
            qx:true,
        }
    },
    methods: {
        zj(index){
           this.arr[index].num+=1
        },
        js(index){
           this.arr[index].num-=1
           if (this.arr[index].num<1) {
            this.arr[index].num=1
            alert('每件商品最少要买一件')
           }
        },
        sc(index){
            this.arr.splice(index,1)
            this.qx2()
        },
       qx2(){
            let n = 0
            this.arr.forEach((item,index)=>{
                if (item.bol) {
                    n+=1;
                }
            })
            if (n == this.arr.length) {
                this.qx = true;
            }else if (n != this.arr.length){
                this.qx = false
                console.log(this.qx);
            }
        },
      aa(){
        if (this.qx==false) {
            this.arr.forEach((item,index)=>{
                    item.bol = false
                })
        }
      }
        
    },
    computed:{
        total(){
            let t = 0;
            this.arr.forEach((item,index)=>{
                if(item.bol){
                    t += item.num*item.dj;
                }
            });
            return t;
        },
    },
    watch:{
        qx(newVal,oldVal){
            if (newVal) {
                this.arr.forEach((item,index)=>{
                    item.bol = true
                })
            }
      }
    }
}).mount('#app')
</script>
相关推荐
好好研究1 小时前
使用JavaScript实现轮播图的自动切换和左右箭头切换效果
开发语言·前端·javascript·css·html
paopaokaka_luck2 小时前
基于Spring Boot+Vue的吉他社团系统设计和实现(协同过滤算法)
java·vue.js·spring boot·后端·spring
伍哥的传说5 小时前
Radash.js 现代化JavaScript实用工具库详解 – 轻量级Lodash替代方案
开发语言·javascript·ecmascript·tree-shaking·radash.js·debounce·throttle
程序视点5 小时前
IObit Uninstaller Pro专业卸载,免激活版本,卸载清理注册表,彻底告别软件残留
前端·windows·后端
前端程序媛-Tian5 小时前
【dropdown组件填坑指南】—怎么实现下拉框的位置计算
前端·javascript·vue
iamlujingtao5 小时前
js多边形算法:获取多边形中心点,且必定在多边形内部
javascript·算法
嘉琪0015 小时前
实现视频实时马赛克
linux·前端·javascript
烛阴6 小时前
Smoothstep
前端·webgl
若梦plus6 小时前
Eslint中微内核&插件化思想的应用
前端·eslint
爱分享的程序员6 小时前
前端面试专栏-前沿技术:30.跨端开发技术(React Native、Flutter)
前端·javascript·面试