zoukankan      html  css  js  c++  java
  • vue

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <link rel="stylesheet" href="style.css">
        </head>
        <body>   
            <div id="app">
                <div v-if="books.length">
                    <table border="" cellspacing="" cellpadding="">
                        <thead>
                            <tr>
                                <th></th>
                                <th>书籍名称</th>
                                <th>出版日期</th>
                                <th>价格</th>
                                <th>购买数量</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="(item,index) in books">
                                <td>{{item.id}}</td>
                                <td>{{item.name}}</td>
                                <td>{{item.data}}</td>
                                <td>{{item.price | showPrice}}</td<!--过滤器 -->
                                <td>
                                    <button @click="decrement(index)" v-bind:disabled="item.count <= 1">-</button>
                                    {{item.count}}
                                    <button @click="increment(index)">+</button>
                                </td>
                                <td>
                                    <button @click="remove(index)">删除</button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <h2>
                        总价格:{{totolPrice | showPrice}}
                    </h2>
                </div>
                <h2 v-else>购物车为空</h2>
            </div>
        </body>
        <script type="text/javascript" src="../vue.js"></script>
        <script src="index.js"></script>
    </html>

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    table{
        border1px solid #ccc;
        border-collapsecollapse;
        border-spacing0;
    }
    th,td{
        padding8px 16px;
        border1px solid #ccc;
        text-alignleft;
    }
    th{
        background-color#e7e7e7;
        color: darkgray;
        font-weight600;
    }

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    const app = new Vue({
        el:'#app',
        data:{
            books:[
                    {id:100,name:'计算机组成原理',data:2002,price:100,count:1},
                    {id:101,name:'现代操作系统',data:2002,price:100,count:1},
                    {id:102,name:'Unix编程艺术',data:2002,price:100,count:1},
                    {id:103,name:'代码大全',data:2002,price:100,count:1},
                ],
        },
        methods:{
            increment(index){
                this.books[index].count++;
            },
            decrement(index){
                this.books[index].count--;
            },
            remove(index){
                this.books.splice(index,1)
            }
        },
        //计算属性
        computed:{
            totolPrice(){
                //第一种写法
                let totolPrice = 0;
                for(let i=0;i<this.books.length;i++){
                    totolPrice += this.books[i].count * this.books[i].price;
                }
                return totolPrice;
                 
                //第二种写法
                // let totolPrice = 0;
                // for(let i in this.books){
                //  totolPrice += this.books[i].count * this.books[i].price;
                // }
                // return totolPrice;
                 
                //第三种写法
                // let totolPrice = 0;
                // for(item of this.books){
                //  totolPrice += item.count * item.price;
                // }
                // return totolPrice;
            }
        },
        //过滤器
        filters:{
            showPrice(price){
                return '¥' + price.toFixed(2);
            }
        }
    })

      

  • 相关阅读:
    java jdbc笔记整理
    Spring IOC (构造器注入)
    WEB-INF目录与META-INF目录的作用[转]
    [转]LL(1)文法判别之First集合、Follow集合、Select集合求法
    java读取TXT文件的方法 (转)
    Ubuntu 12.04下搭建Web服务器 (MySQL+PHP+Apache)(转)
    题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
    error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCR...(转)
    汇编leal命令的灵活用法及理解
    C++之继承
  • 原文地址:https://www.cnblogs.com/1014852131qq/p/14106951.html
Copyright © 2011-2022 走看看