vue全选和反选 vue实现简单全选和反选功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

vue全选和反选 vue实现简单全选和反选功能

weixin_45803990   2021-03-16 我要评论
想了解vue实现简单全选和反选功能的相关内容吗,weixin_45803990在本文为您仔细讲解vue全选和反选的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Vue,全选,反选,下面大家一起来学习吧。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    table {
      width: 700px;
      text-align: center;
    }
    tr,
    th {
      height: 40px;
    }
  </style>
  <script src="../vue.js"></script>
</head>

<body>
  <div class="box">
    <table cellspacing='0' border="solid 1px">
      <thead>
        <tr>
          <th>全选<input type="checkbox" v-model='isAllChecked'></th>
          <th>id</th>
          <th>商品名称</th>
          <th>商品价格</th>
          <th>商品数量</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for='item in goods'>
          <td><input type="checkbox" v-model='item.isCheck'></td>
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>{{item.price}}</td>
          <td>{{item.num}}</td>
        </tr>
      </tbody>
    </table>
  </div>

  <script>
    var vm = new Vue({
      el: '.box',
      methods: {
      },
      data: {
        goods: [
          {
            id: 20200905,
            name: '苹果',
            price: 3,
            num: 12,
            isCheck: false,
          },
          {
            id: 20200905,
            name: '香蕉',
            price: 2,
            num: 33,
            isCheck: false,
          },
          {
            id: 20200905,
            name: '橘子',
            price: 4,
            num: 44,
            isCheck: false,
          },
        ]
      },
      computed: {

        isAllChecked: {
          /* 
           this.goods.every(el=>el.isCheck)返回结果为true 或者false

          遍历下方每一个isCheck的状态、
            1、 都选中返回true---------即全选为true,
            2、 有一个没选中返回false---即全选为false
          */
          get() {
            return this.goods.every(el => el.isCheck)
          },
          set(val) {
            // 全选的状态true、false两种状态
            console.log(val);

            // val为true即全选的时候、下方每一个isCheck也是true
            // val为false即全选的时候、下方每一个isCheck也是false
            return this.goods.forEach(el => el.isCheck = val);
          }
        }
      }
    })
  </script>

</body>

</html>

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们