vue实现多选框(复选框)

本文展示了如何在Vue.js中实现表格的全选和多选功能,通过使用v-for循环遍历数据,结合checkbox状态管理和点击事件,完成选中与反选操作。示例代码详细解释了数据绑定和事件监听在组件交互中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
    <table class="t1">
                <tr class="tr1">
                    <td>
                        <input type='checkbox' :checked="clsAll.length === pojectClsAll.length" @click='checkedAll()'>全选
                    </td>
                    <td v-for="(cls,index) in pojectClsAll" :key="index">
                        <input type='checkbox' :checked="clsAll.indexOf(cls.cid)>=0" @click='checkedOne(cls.cid)'>{{cls.cname}}
                    </td>
                </tr>
            </table>
</template>
data(){
        return{
            clsAll:['0','1','2','3','4','5','6','7','8','9','10','11','12','13'],
            pojectClsAll:"",
            isCheckedAll: false
        }
    },
methods:{
        checkedOne (id) {
            let idIndex = this.clsAll.indexOf(id)
            if (idIndex >= 0) {
            // 如果已经包含了该id, 则去除(单选按钮由选中变为非选中状态)
            this.clsAll.splice(idIndex, 1)
            } else {
            // 选中该checkbox
            this.clsAll.push(id)
            }
            console.log(this.clsAll);
        },
        checkedAll () {
            this.isCheckedAll = !this.isCheckedAll
            if (this.isCheckedAll) {
                // 全选时
                this.clsAll = []
                this.pojectClsAll.forEach(function (fruit) {
                this.clsAll.push(fruit.cid)
                }, this)
            } else {
                this.clsAll = []
            }
            console.log(this.clsAll);
        },
}

效果:

 可以通过console.log()来打印数组

1.多选效果

全选效果 

感谢观看,感觉有帮助的感谢大家点赞收藏,不懂的可以私信,大家一起来解决问题!! 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值