国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 16:03:08
字體:
來源:轉載
供稿:網友

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

<template>  <div>    <table>      <tr>        <td><input type="checkbox" v-model="checkAll">全選({{checkedCount}})</td>        <td>產品名稱</td>        <td>價格</td>        <td>數量</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> 

效果:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 噶尔县| 应城市| 石楼县| 积石山| 钟祥市| 进贤县| 六安市| 宁明县| 枣庄市| 甘孜县| 遂川县| 大冶市| 孝昌县| 阿拉善右旗| 汤原县| 青田县| 增城市| 洱源县| 永昌县| 灵山县| 永兴县| 镇宁| 永州市| 康马县| 鄂尔多斯市| 长武县| 潢川县| 高陵县| 定襄县| 赤峰市| 壤塘县| 都安| 大姚县| 松溪县| 大庆市| 赤峰市| 四子王旗| 封丘县| 延安市| 镇巴县| 喀什市|