vue手动实现全选,单选

2 篇文章 0 订阅

html

<table class="fasong-box">
						<colgroup>
							<col width="20%" v-if="is.active == '0'">
							<col width="25%">
							<col width="25%">
						</colgroup>
						<thead>
							<tr>
								<td v-if="is.active == '0'">
									<input :checked="is.checked" type="checkbox" @click="quanxuan" />
								</td>
								<td>
									姓名
								</td>
								<td>
									部门
								</td>
							</tr>
						</thead>
						<tbody>
							<tr v-for="i in chayue">
								<td v-if="is.active == '0'">
									<input :checked="i.check" type="checkbox" @click="xz(i)" />
								</td>
								<td>
									{{i.name}}
								</td>
								<td>
									{{i.orgDept}}
								</td>
							</tr>
						</tbody>
					</table>

变量

is: {
							checked:false
},		
dingList:[]

执行js

xz:function(item){
							item.check = !item.check
							this.xinzeng(item)
							this.quan()
						},
						quanxuan:function(){
							var list = JSON.parse(JSON.stringify(this.chayue))
							if(this.is.checked){
								for(var r in list){
									list[r].check = false
									this.xinzeng(list[r])
								}
							}else{
								for(var i in list){
									list[i].check = true
									this.xinzeng(list[i])
								}
							}
							this.chayue = list;
							this.quan()
						},
						xinzeng:function(item){
							var boo = false,sc = false,num = 0
							if(this.dingList.length){
								for(var i in this.dingList){
									d = this.dingList[i]
									if(item.check){
										if(d.id == item.id){
											boo = true
										}
									}else{
										if(d.id == item.id){
											sc = true
											num = i
										}
									}
								}
								if(sc){
									this.dingList.splice(num,1)
								}else{
									if(!boo){
										this.dingList.push(item)
									}
								}
							}else{
								if(item.check){
									this.dingList.push(item)
								}
							}
						},
						quan:function(){
							var boo=false
							if(this.chayue.length){
								for(var i in this.chayue){
									var d = this.chayue[i]
									if(!d.check){
										boo = true
									}
								}
							}
							if(boo){
								this.is.checked = false
							}else{
								this.is.checked=true
							}
						},

getList的时候执行

self.is.checked = false;
for(var i in self.chayue){
	self.chayue[i].check = false
}

原理是:
拿到list的时候把全选与单个选项设置成不选中。
每次单选的时候检测是否全选。全选的概念为当前list的check全部为true。
把单选的item增加到特定的变量

全选的时候与上面思路相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值