zoukankan      html  css  js  c++  java
  • Vue小案例测试-------------------实现购物车小模块

    1.html代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Vue小案例</title>
      <link rel="stylesheet" href="style.css">
    </head>
    <body>
            <div id="app">
              <div v-if="this.books.length>0">
                <table>
                  <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 | finalPrice}}</td>
                    <td>
                      <button @click="addCount(index)" :disabled="item.count<1">+</button>
                      {{item.count}}
                      <button @click="SubCount(index)" :disabled="item.count<=1">-</button>
                    </td>
                    <td>
                      <button @click="DelItem">删除</button>
                    </td>
                  </tr>
                  </tbody>
                </table>
                <h3>你一共消费了{{TotalPrice | finalPrice}}</h3>
              </div>
              <h3 v-else>你的购物车为空</h3>
            </div>
    <script src="../js/vue.js"></script>
    <script src="index.js"></script>
    </body>
    </html>

    2.Css样式

    table {
        border:1px solid #e9e9e9;
        border-collapse: collapse;
        border-spacing: 0;
    }
    
    th,td{
        padding: 8px 16px;
        border: 1px solid #e9e9e9;
        text-align: left;
    }
    
    th{
        background-color: #f7f7f7;
        color: #5c6b77;
        font-weight: 700;
    }

    3.Vue.js代码

    const app=new Vue({
        el:"#app",
        data:{
            books:[
                {
                    id:1,
                    name:'计算机算法',
                    data: '2002-2',
                    price:29.00,
                    count:1
                },
                {
                    id:2,
                    name:'科学算法',
                    data: '2012-2',
                    price:37.00,
                    count:1
                },
                {
                    id:3,
                    name:'深爱与你',
                    data: '2028-2',
                    price:86.00,
                    count:1
                },{
                    id:4,
                    name:'美女与野兽',
                    data: '2066-2',
                    price:28.00,
                    count:1
                }
            ]
        },
        methods:{
            addCount(index){
                this.books[index].count++
            },
            SubCount(index){
                this.books[index].count--
            },
            DelItem(index){
                this.books.splice(index,1)
            }
        },
        computed:{
            TotalPrice(){
                let resultPrice=0;
                for (let i=0;i<this.books.length;i++){
                    resultPrice+=this.books[i].price*this.books[i].count
                }
                 return resultPrice;
            }
        },
        filters:{
            finalPrice(price){
                return '¥'+price.toFixed(2)
            }
        }
    })

  • 相关阅读:
    每日一水 POJ8道水题
    编译和使用 MySQL C++ Connector
    j2ee model1模型完成分页逻辑的实现 详解!
    DB查询分析器访问EXCEL时,要在表名前后加上中括弧或双引号
    指向结构体变量的指针
    EOSS V3.0 企业运营支撑系统(基于RBAC原理的权限管理)
    MybatisGen1.0 Mybatis JavaBean Mapper生成工具
    The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by
    资源-Android:Android
    软件-开发软件:Android Studio
  • 原文地址:https://www.cnblogs.com/skyfail/p/14660031.html
Copyright © 2011-2022 走看看