2018-09-20组件 购物车

1.配合bootstrap使用的购物车

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
   <div id='app'>
       <my-father></my-father>
   </div>
    <script src='js/vue.js'></script>
    <script>
        Vue.component('my-father',{
            template:`
              <div class='container'>
               <table class='table table-bordered text-center'>
                  <thead>
                      <tr>
                         <th class='text-center'>编号</th>
                         <th class='text-center'>名称</th>
                         <th class='text-center'>单价</th>
                         <th class='text-center'>数量</th>
                         <th class='text-center'>小计</th>
                       </tr>
                  </thead>
                   <my-child v-bind:product='goods'></my-child>
               </table>
             </div>
            `,
            data:function(){
                return{
                    goods:[
                        {pname:'apple',price:3,count:1,sub:3},
                        {pname:'pear',price:4,count:1,sub:4},
                        {pname:'orange',price:5,count:1,sub:5}
                    ]
                }
            }
        })
       
        Vue.component('my-child',{
            props:['product'],
            template:`
                <tbody>
                  <tr v-for="(value,index) in product">
                     <td>{{index+1}}</td>
                     <td>{{value.pname}}</td>
                     <td>{{value.price}}</td>
                     <td>
                          <button @click='add(index)'>+</button>
                          <span>{{value.count}}</span>
                         <button @click='redu(index)'>-</button> 
                      </td>
                     <td>{{value.sub}}</td>
                  </tr>
                  <tr>
                      <td colspan=5>总价:{{sum}}</td>
                  </tr>
                </tbody>
            `,
            data:function(){
                return{
                    sum:12
                }
            },
            methods:{
                add:function(ind){
                    this.product[ind].count++;
                    //小计、
                    this.product[ind].sub=this.product[ind].count*this.product[ind].price;
                    this.countSum();
                     
                },
                redu:function(ind){
                    if(this.product[ind].count>1){
                       this.product[ind].count--;
                    }
                    //小计
                      this.product[ind].sub=this.product[ind].count*this.product[ind].price;
                     this.countSum();
                },
                countSum:function(){
                    for(var i=0,total=0;i<this.product.length;i++){
                        total+=this.product[i].sub;
                    }
                    this.sum=total;
                }
                
                
            }
        })
        
        
        new Vue({
            el:'#app'
        })
    </script>
</body>
</html>

样式如下:


1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,074评论 3 119
  • 2018年2月13日,清河林业地区公安局小古洞交警中队在春节来临之际,针对小古洞辖区,探亲访友、外出购物、...
    asfgsdgs阅读 1,558评论 0 0
  • 如何有效管理情绪 开篇语:要成为情绪的主人而不要成为情绪的奴隶 一.何为情绪与情绪管理 情绪,是对一系列主观认知经...
    wekang阅读 1,324评论 0 1
  • 我想做一棵驻在海边的树 让海水滋润我灵魂 白色的帆来到海边 海带着它远去 我静静的守在原来的地方 我知道它会回来 ...
    短叹嗯阅读 3,570评论 4 13

友情链接更多精彩内容