• 购物车实现单选全选,计算数量和价格


    <template>
      <div class="login">
        <div class="item" v-for="(item,index) in dataList" :key="index">
          <input @change="change(index,item.checked)" type="checkbox" :checked="item.checked" />
          {{item.name}},单价{{item.price}}个数{{item.num}}
        </div>
        <div class="item">
          <input type="checkbox" @change="changeAll" :checked="allChecked" />
          全选,总数量{{count}}总价格{{money}}
        </div>
      </div>
    </template>
    <script>
    export default {
      name: "login",
      data() {
        return {
          dataList: [
            { id: 1, name: "苹果", price: 4, num: 4 },
            { id: 2, name: "香蕉", price: 3, num: 2 },
            { id: 3, name: "梨", price: 3, num: 2 },
            { id: 4, name: "西红柿", price: 2, num: 11 },
          ],
          allChecked: false,
          count: 0,
          money: 0,
        };
      },
      methods: {
        changeAll() {
          this.count = 0;
          this.money = 0;
          for (var i = 0; i < this.dataList.length; i++) {
            this.$set(this.dataList[i], "checked", false);
          }
          if (this.allChecked) {
            this.allChecked = false;
            this.count = 0;
            this.money = 0;
          } else {
            this.allChecked = true;
            for (var j = 0; j < this.dataList.length; j++) {
              this.count += Number(this.dataList[j].num);
              this.money +=
                Number(this.dataList[j].num) * Number(this.dataList[j].price);
              this.$set(this.dataList[j], "checked", true);
            }
          }
        },
        change(index, checked) {
          this.count = 0;
          this.money = 0;
          console.log(checked,887)
          if (!checked) {
            this.$set(this.dataList[index], "checked", true);
          } else {
            this.$set(this.dataList[index], "checked", false);
          }
          this.$nextTick(() => {
            for (var j = 0; j < this.dataList.length; j++) {
              if (this.dataList[j].checked) {
                this.count += Number(this.dataList[j].num);
                this.money +=
                  Number(this.dataList[j].num) * Number(this.dataList[j].price);
              }
            }
            if (this.dataList.every((item) => item.checked == true)) {
              this.allChecked = true;
            } else {
              this.allChecked = false;
            }
          });
        },
      },
    };
    </script>
    <style lang="scss">
    .login {
      margin: 100px;
    }
    .item {
      display: flex;
    }
    </style>
  • 相关阅读:
    引用数据类型:字符串和数组
    java流程控制
    java基本类型-八大基本数据类型(4类8种)
    Java标识符
    Java注释
    虚拟环境管理virtualenv
    pipenv管理模块和包
    有关线程的说法?
    TCP三次握手的序列号和确认号的计算
    jenkins的安装
  • 原文地址:https://www.cnblogs.com/studyWeb/p/13432283.html
Copyright © 2020-2023  润新知