本文实例为大家分享了react实现复选框全选和反选组件的具体代码,供大家参考,具体内容如下
运行效果图如下:
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
import React, { Component } from 'react' ;
import { withRouter } from 'react-router-dom' ;
// import Menu from '../menu/Menu.jsx';
class List extends Component {
constructor () {
super ();
this .state = {
title: '我是List的标题' ,
content: '我是内容部分部分' ,
chooseAll: false , // 全选标志
inters: [ 'bsball' , 'ymball' , 'fbball' ], // 页面加载默认选中项
intersAll: [ 'bsball' , 'ymball' , 'ppball' , 'fbball' ],
fchoose: false // 正反选标志
};
}
// 全选
chooseAll (event) {
let {checked, value} = event.target;
let chooseAll = this .state.inters.length === 4 ? true : false ;
let inters = [ 'bsball' , 'ymball' , 'ppball' , 'fbball' ];
checked ? (
this .setState({
inters,
chooseAll: checked
})
) : (
this .setState({
inters: [],
chooseAll: checked
})
);
}
// 点击复选框
chooseInter (event) {
let val = event.target.value;
let checked = event.target.checked;
let {inters} = this .state;
// event.stopPropagation();
// 选中复选框并且值不在数组里面
if (checked && ! this .state.inters.includes(val)) {
inters.push(val);
} else {
// 取消选中的选项
inters = inters.filter(v => val !== v);
}
let chooseAll = inters.length === 4 ? true : false ;
console.log(inters);
this .setState({
inters,
chooseAll
});
}
// 反选处理
fchooseHandle (event) {
let {checked, value} = event.target;
let {inters, intersAll} = this .state;
let chooseAll = this .state.inters.length === 4 ? true : false ;
let arr = []; // 反选结果
this .setState({
fchoose: checked,
chooseAll
});
intersAll.forEach(item => {
if (!inters.includes(item)) {
arr.push(item);
}
});
this .setState({
inters: arr
});
}
componentWillMount () {
let chooseAll = this .state.inters.length === 4 ? true : false ;
this .setState({
chooseAll
});
}
render () {
return (
<div className= "list" >
{ /* <Menu /> */ }
<h1>{ this .state.title}</h1>
<p>{ this .state.content}</p>
<p>
<label>
<input type= "checkbox" value= "bsball" checked={ this .state.fchoose} onClick={ this .fchooseHandle.bind( this )}/>{ this .state.fchoose ? '取消反选' : '反选' }
</label>
<label>
<input type= "checkbox" value= "bsball" checked={ this .state.chooseAll} onClick={ this .chooseAll.bind( this )}/>{ this .state.chooseAll ? '取消全选' : '全选' }
</label>
</p>
<p>
<label>
<input type= "checkbox" value= "bsball" checked={ this .state.inters.includes( 'bsball' )} onClick={ this .chooseInter.bind( this )}/>篮球
</label>
<label>
<input type= "checkbox" value= "ymball" checked={ this .state.inters.includes( 'ymball' )} onClick={ this .chooseInter.bind( this )}/>羽毛球
</label>
<label>
<input type= "checkbox" value= "ppball" checked={ this .state.inters.includes( 'ppball' )} onClick={ this .chooseInter.bind( this )}/>兵乓球
</label>
<label>
<input type= "checkbox" value= "fbball" checked={ this .state.inters.includes( 'fbball' )} onClick={ this .chooseInter.bind( this )}/>足球
</label>
</p>
</div>
);
}
}
export default withRouter(List);
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/CodingNoob/article/details/88106500