vue模拟购物车

效果图

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>购物车</title>
    <script src="vue.js"></script>
    <style>
       h2 {
           text-align: center;
        }

       tbody tr td .activess{
            background-color: #35ff71;
           border:1px solid transparent;
        }

    </style>
    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
    <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div id="box">
    <h2>书籍登录</h2>
    <Msg></Msg>



</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script>
    //点击添加

    //th
    Vue.component('Btn', {
        template: `<th>

<slot></slot>
</th>`
    })

    //th部分
    let Vhead = {

        template: `<thead>
<tr>
<Btn style="text-align:center">属性</Btn>
<Btn style="text-align:center">名字</Btn>
<Btn style="text-align:center">作者</Btn>
<Btn style="text-align:center">多少</Btn>
<Btn style="text-align:center">价格</Btn>
<Btn style="text-align:center">删除</Btn>
</tr> </thead>`
    }
    let Vbody = {
        data() {
            return {
                isindex:0,
                age:'',

                tp:0,
            }
        },props:['arr'], template: `<tbody>
<tr v-for="(item,index) of arr">
<td>{{index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.author}}</td>
<td><button v-bind:disabled="item.age === 0"  type="button" class="btn btn-primary" @click="item.age-=1">-</button> {{item.age}} <button  type="button" class="btn btn-primary" @click="item.age+=1">+</button></td>
<td>{{item.price*item.age}}</td>
<td><button type="button" class="btn btn-primary" @click="del(index)" :class="{activess:index%2==0}">删除</button></td>
 </tr>

        <td colspan="6" align="right"><h2 style="text-align:right;">所有商品的总价¥{{totalpeice}}</h2></td>


 </tbody>`, methods: {

          del(index) {
                this.arr.splice(index,1);

            },

        },computed:{
            totalpeice:function () {
                var tprice=0;
                for(let i=0;i<this.arr.length;i++){
                    tprice+=this.arr[i].price*this.arr[i].age;
                    tprice.toFixed(2)
                }
                return tprice;
            }
        }
        }

    let Msg = {
        data(){
            return{
                name:'',
                author:'',
                age:'',
                price:'',
                arr: [
                    { name: '西游记', author: '吴承恩', age: 1, price: 10},
                    { name: '红楼梦', author: '曹雪芹', age: 1, price: 23},
                    { name: '水浒传', author: '施耐庵', age: 1, price: 2},
                ],
            }
        },
        template: `<div>
 <table class="table table-hover" style="text-align: center">
<Vhead></Vhead>
<Vbody :arr="arr"></Vbody>
</table>
 <form>

        <div class="form-group">
            <label for="exampleInputEmail1">书名</label>
            <input type="text" class="form-control" id="exampleInputEmail1" placeholder="书名" v-model="name" required>
        </div>
        <div class="form-group">
            <label for="exampleInputEmail2">作者</label>
            <input type="text" class="form-control" id="exampleInputEmail2" placeholder="作者" v-model="author" required>
        </div>
        <div class="form-group">
            <label for="exampleInputEmail3">多少</label>
            <input type="number" class="form-control" id="exampleInputEmail3"  placeholder="多少" v-model="age" required>
        </div>
        <div class="form-group">
            <label for="exampleInputEmail4">价格</label>
            <input type="number" class="form-control" id="exampleInputEmail4"  placeholder="价格" v-model="price" required>
        </div>
        <p style="width:100%">
    <button type="button" class="btn btn-primary" style="width:100%" @click="add" id="btn">提交</button>

</p>
    </form>
</div> `, components: {
            Vhead,
            Vbody,
        },
        methods:{
            add(){
                if(this.age!=''&&this.name!=''&&this.price!=''&&this.author!=''){
                    this.arr.push({
                        age:parseInt(this.age),
                        price:parseInt(this.price),
                        name:this.name,
                        author:this.author,
                    })
                }else{
                    alert('不能为空');
                }

                this.reset()
            }, reset(){
                 this.name = '',
                 this.age = '',
                 this.price = '',
                 this.author = ''


        },
        }

    }
    new Vue({
        el: '#box',
        data:{
        },
        methods:{

        },
        components: {
            Msg
        },

    })
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值