[英]On click of a checkbox, how to set all previous checkbox as checked in angular 4
I have few check boxes having label as (Beginner,Intermediate, Advanced, Expert etc..) 我有几个复选框的标签为(初学者,中级,高级,专家等。)
Now I want on click of any checkbox , all previous checkbox should checked. 现在,我要单击任何复选框,所有先前的复选框都应选中。
Ex : If I click on Advanced, only all previous check boxes should get checked except the expert. 例如:如果我单击“高级”,则除专家外,仅所有以前的复选框应被选中。
Below is the code to get the list of check boxes. 下面是获取复选框列表的代码。 Please let me know if any solution is there.
请让我知道是否有解决方案。
<div class="cb-cont" *ngFor = "let circle of [1,2,3,4], let i = index;" >
<input type="checkbox" class="cb" [attr.id]="'vacancy' + i" (change)="changeCheckbox(i)" [checked]=" (i==0) ? true : false" >
<label [attr.for]="'vacancy' + i" ><i class="xico-completed"></i></label>
</div>
changeCheckbox(i) {
var elem = <HTMLInputElement>event.target
elem.checked = true;
}
You can store the index of the selected checkbox and select all the previous ones. 您可以存储所选复选框的索引,然后选择所有先前的复选框。 Like this:
像这样:
<div class="cb-cont" *ngFor = "let circle of [1,2,3,4], let i = index;" >
<input type="checkbox" class="cb" [attr.id]="'vacancy' + i" (change)="changeCheckbox(i)" [checked]=" (i<=selectedCheckbox) ? true : false" >
<label [attr.for]="'vacancy' + i" ><i class="xico-completed"></i></label>
</div>
And in the controller: 并在控制器中:
selectedCheckbox = 0;
public changeCheckbox(i) {
this.selectedCheckbox = i;
var elem = <HTMLInputElement>event.target;
elem.checked = true;
}
Here you have a working stackblitz with the code: https://stackblitz.com/edit/angular-a29qko 在这里,您可以使用代码进行有效的stackblitz: https ://stackblitz.com/edit/angular-a29qko
Updated Gobli's Answer 更新了哥布利的答案
component 零件
selectedCheckbox = 0;
k = 0;
public changeCheckbox(i) {
debugger;
this.selectedCheckbox = i;
var elem = <HTMLInputElement>event.target;
if(elem.checked){
this.k=1;
elem.checked = true;
}else{
this.k=0;
elem.checked = false;
}
}
Html HTML
<div class="cb-cont" *ngFor = "let circle of [1,2,3,4], let i = index;" >
<input type="checkbox" class="cb" [attr.id]="'vacancy' + i"
(change)="changeCheckbox(i)" [checked]="(k==1) ?( (i<=selectedCheckbox) ? true : false ) :( (i>=selectedCheckbox) ? false : true ) " >
<label [attr.for]="'vacancy' + i" ><i class="xico-completed"></i>
hope this helps u. 希望这对你有帮助。 https://angular-2hh27h.stackblitz.io/
https://angular-2hh27h.stackblitz.io/
COMPONENT 零件
private checked: number = 0;
changeCheckbox(index, e) {
if (index === 0) {
e.target.checked = true;
this.checked = 0;
return;
}
if (e.target.checked) {
if ((this.checked+1) === index) {
this.checked = index;
}else {
e.target.checked = false;
}
} else {
e.target.checked = true;
this.checked = index;
}
}
getChecked(i) {
return (i <= this.checked);
}
HTML HTML
<div class="cb-cont" *ngFor = "let circle of [1,2,3,4], let i = index;" >
<input type="checkbox" class="cb" [attr.id]="'vacancy' + i" (change)="changeCheckbox(i, $event)" [checked]="getChecked(i)" >
<label [attr.for]="'vacancy' + i" ><i class="xico-completed"></i></label>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.