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'>數(shù)量</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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,210評論 3 119
  • 2018年2月13日,清河林業(yè)地區(qū)公安局小古洞交警中隊在春節(jié)來臨之際,針對小古洞轄區(qū),探親訪友、外出購物、...
    asfgsdgs閱讀 233評論 0 0
  • 如何有效管理情緒 開篇語:要成為情緒的主人而不要成為情緒的奴隸 一.何為情緒與情緒管理 情緒,是對一系列主觀認(rèn)知經(jīng)...
    wekang閱讀 189評論 0 1
  • 我想做一棵駐在海邊的樹 讓海水滋潤我靈魂 白色的帆來到海邊 海帶著它遠(yuǎn)去 我靜靜的守在原來的地方 我知道它會回來 ...
    短嘆嗯閱讀 594評論 4 13

友情鏈接更多精彩內(nèi)容