走自己的路,让别人说去吗!美好的日子带来快乐,阴暗的日子带来经验,所以别对任何一天怀有遗憾。
本文实例为大家分享了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>
本文vue如何实现简单全选和反选功能到此结束。好好去爱,去生活。青春如此短暂,不要叹老。偶尔能够停下来休息,可是别蹲下来张望。走了一条路的时候,记得别回头看。时不时问问自我,自我在干嘛?记住,每一天的太阳都是新的,不要辜负了完美的晨光。小编再次感谢大家对我们的支持!