最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue2.0中vue-cli實現(xiàn)全選、單選計算總價格的實例代碼

 更新時間:2017年07月18日 09:47:01   作者:pearl007  
本篇文章主要介紹了vue2.0中vue-cli實現(xiàn)全選、單選計算總價格的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

由于工作的需要并鑒于網(wǎng)上的vue2.0中vue-cli實現(xiàn)全選、單選方案不合適,自己寫了一個簡單實用的。就短短的126行代碼。

<template>

  <div>

    <table>

      <tr>

        <td><input type="checkbox" v-model="checkAll">全選({{checkedCount}})</td>

        <td>產品名稱</td>

        <td>價格</td>

        <td>數(shù)量</td>

      </tr>

      <tr v-for="(item,$index) in lists">

        <td><span v-show="checkedCount===lists.length || item.checked===true">我被選中</span><input type="checkbox" :value="item.id" v-model="checked" @click="currClick(item,$index)"></td>

        <td>{{item.productName}}</td>

        <td>{{item.price}}</td>

        <td>{{item.count}}</td>

      </tr>

      <tr>

        總價:{{totalMoney}}

      </tr>

    </table>

  </div>

</template>

<script>

  export default{

    data() {

      return {

        checked:[],

        totalPrice:[],

        lists : [

          {

            productName:'產品1',

            price:'24',

            count:'3',

            id:1

          },

          {

            productName:'產品2',

            price:'25',

            count:'6',

            id:2

          },

          {

            productName:'產品3',

            price:'54',

            count:'7',

            id:3

          }

        ]

      }

    },

    computed:{

      totalMoney:function(item,index){

        let sum = 0;

        for(let i=0;i<this.totalPrice.length;i++){

          sum += this.totalPrice[i];

        };

        return sum;

      },

      checkAll: {

        get: function() {

          return this.checkedCount == this.lists.length;

        },

        set: function(value){

          var _this = this;

          if (value) {  

            this.totalPrice = [];

            this.checked = this.lists.map(function(item) {

              item.checked = true;

              let total = item.price*item.count;

              _this.totalPrice.push(total);

              return item.id

            })

          }else{

            this.checked = [];

            this.totalPrice=[];

            this.lists.forEach(function(item,index){

              item.checked = false;

            });

          }

        }

      },

      checkedCount: {

        get: function() {

          return this.checked.length;

        }

      }

    },

    methods:{

      currClick:function(item,index){

        var _this = this;

        if(typeof item.checked == 'undefined'){

          this.$set(item,'checked',true);

            let total = item.price*item.count;

            this.totalPrice.push(total);

            console.log(this.totalPrice);

        }else{

          item.checked = !item.checked;

          if(item.checked){

            this.totalPrice = [];

            this.lists.forEach(function(item,index){

              if(item.checked){

                let total = item.price*item.count;

                _this.totalPrice.push(total);

              }

            });

          }else{

            this.totalPrice = [];

            this.lists.forEach(function(item,index){

              if(item.checked){

                let total = item.price*item.count;

                _this.totalPrice.push(total);

              }

            });

          }

        }

      }

    }

  }

</script>

<style>

  tr td{

    width:200px;

    background: #eee;

    padding:10px 0;

  }

 

</style> 

效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

旬邑县| 荥阳市| 太原市| 咸阳市| 广西| 内丘县| 托克逊县| 开封市| 冕宁县| 泸溪县| 湖南省| 南昌县| 大埔县| 定州市| 湖州市| 平塘县| 和平县| 泗水县| 禄劝| 东光县| 昌吉市| 伊金霍洛旗| 昌黎县| 称多县| 炉霍县| 阜宁县| 库伦旗| 忻城县| 历史| 铅山县| 石景山区| 清徐县| 铜陵市| 浦城县| 始兴县| 柳江县| 茶陵县| 阜新市| 鄄城县| 洛扎县| 曲沃县|