vue小练习之全反选案例

2023-12-18 09:27:59
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <div>
      <span>全选:</span>
      <input type="checkbox" v-model="isAll" />
      <button @click="reverseBtn">反选</button>
      <ul>
        <li v-for="(item,index) in arr" :key="index">
          <input type="checkbox"  v-model="item.checked"/>
          <span>{{item.name}}</span>
        </li>
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          {
            name: "猪八戒",
            checked: false,
          },
          {
            name: "孙悟空",
            checked: false,
          },
          {
            name: "唐僧",
            checked: false,
          },
          {
            name: "白龙马",
            checked: false,
          },
        ],
      },
      computed:{
        isAll:{
            get(){
                return this.arr.every(item=>item.checked)
            },
            set(value){
                this.arr.forEach(item=>item.checked=value)
            }

        }

      },
      methods:{
        reverseBtn(){
            this.isAll=!this.isAll
        }
      }
    })
  </script>


</body>

</html>

文章来源:https://blog.csdn.net/m0_56713342/article/details/135054462
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。