42-图书购物车案例-事件完成

最终效果如下图:

 完整代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv=" X-UA-Compatible" content="ie=edge">
    <title>42-图书购物车案例-事件完成</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" ></script>
    <style>
        table,td,th{
            border: 1px solid #000;
            /* 设置表格线条粗细,形状,颜色 */
        }
        table{
            border-collapse: collapse; /* 作用:表格边框折叠,没有间隙 */
            /* collapse值作用:如果可能,边框会合并为一个单一的边框。会忽略 border-spacing 和 empty-cells 属性。 */

        }
        td,th{
            padding: 10px; 
            /* 设置表格中单元格间距 */
        }
    </style>
</head>
<body>
    <div id='app'>
        <table>
            <tr>
                <th v-for="item_title in title">{{item_title}}</th>
                <!-- 遍历表格标题 -->
            </tr>
            <tr v-for="item,index in books">
                <!-- 遍历表格内容 -->
                <th>{{index}}</th>
                <th>{{item.name}}</th>
                <th>{{item.date}}</th>
                <th>{{'¥'+item.price.toFixed(2)}}</th>
                <!-- toFixed作用:保留两位小数 -->
                <th>
                    <button @click="redu(index)">-</button>
                    {{item.num}}
                    <button @click="add(index)">+</button>
                </th>
                <th>
                    <button @click="del(index)">移除</button>
                </th>
            </tr>
            <tr>
                <td colspan="6" style="text-align: right;">总价:{{total}}</td>
                <!-- collapse='6'是合并6格 style作用是靠右显示-->
            </tr>
        </table>
    </div>
    <script>
        new Vue({
            el: '#app',
            computed:{
                total(){
                    let ret = this.books.reduce((pre,cur)=>{
                        return pre + cur.price*cur.num
                    },0)
                    return '¥'+ret.toFixed(2)+'元'
                }
            },
            methods:{
                add(i){
                    this.books[i].num++;
                },
                /*
                redu(i){ // 标准写法
                    if(this.books[i].num===1){
                        return
                    }
                    this.books[i].num--;
                }
                */
                redu(i){ // 条件判断写法,三元运算符
                    this.books[i].num>1?this.books[i].num--:''
                },
                del(i){
                    this.books.splice(i,1)
                }
            },
            data:{
                title:['编号', '书籍名称', '出版日期', '价格', '数量', '操作'],
                books:[
                    {
                        name:'算法导论',
                        date:'2006-9',
                        price:85,
                        num:1
                    },
                    {
                        name:'UNIX编程艺术',
                        date:'2006-2',
                        price:59,
                        num:1
                    },
                    {
                        name:'Vue程序设计',
                        date:'2008-10',
                        price:35,
                        num:1
                    },
                    {
                        name:'颈椎康复',
                        date:'2006-3',
                        price:139,
                        num:1
                    }
                ]
            }
        })
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值