[英]How to unchecked all checked checkbox in angular 6
I have a dynamic list of data, and each element is shown as a checkbox. 我有一个动态数据列表,每个元素都显示为一个复选框。 The elements I checked are saved in an array.
我检查的元素保存在数组中。 After I save the array, I want all the checked checkbox to be unchecked when I press a button, but it doesn't work.
保存阵列后,我希望在按下按钮时取消选中所有选中的复选框,但是它不起作用。
I want to unchecked the selected checkbox after I saved the array. 保存阵列后,我想取消选中选中的复选框。 Please help me to find a solution.
请帮助我找到解决方案。 Thanks.
谢谢。
Stackblitz link here Stackblitz链接在这里
export class AppComponent {
userRoleListTemp: any = [];
userRoleListToSave: any = [];
checkedInfo: any;
appUserRoleList: any = [
{id: '1', roleName: 'SETUP_ROLE'},
{id: '2', roleName: 'ENTRY_ROLE'},
{id: '3', roleName: 'SEATPLAN_ROLE'},
{id: '4', roleName: 'MARKSENTRY_ROLE'},
{id: '5', roleName: 'APPLICANT_ROLE'}
];
constructor() {}
onChangeRole(userRole: string, isChecked) {
this.checkedInfo = isChecked;
if (isChecked.target.checked) {
this.userRoleListTemp.push(userRole);
} else {
let index = this.userRoleListTemp.indexOf(userRole);
this.userRoleListTemp.splice(index, 1);
}
}
checkedEvnt() {
this.checkedInfo.target.checked = false;
}
}
<h1>Unchek All Roles</h1>
<div class="form-check" *ngFor="let appUserRole of appUserRoleList">
<input class="form-check-input" name="{{appUserRole.roleName}}"
type="checkbox" id="{{appUserRole.roleName}}"
(change)="onChangeRole(appUserRole.roleName, $event)">
<label class="form-check-label" for="{{appUserRole.roleName}}">
{{appUserRole.roleName}}
</label>
</div>
<button (change)="checkedEvnt()">Uncheck All</button>
<pre>{{ userRoleListTemp | json}}</pre>
Here is the link https://stackblitz.com/edit/angular-me1cdb?file=src/app/app.component.html 这是链接https://stackblitz.com/edit/angular-me1cdb?file=src/app/app.component.html
I am using isChecked
flag it will help you 我正在使用
isChecked
标志,它将为您提供帮助
appUserRoleList: any = [
{id: '1', roleName: 'SETUP_ROLE',isChecked:false},
{id: '2', roleName: 'ENTRY_ROLE',isChecked:false},
{id: '3', roleName: 'SEATPLAN_ROLE',isChecked:false},
{id: '4', roleName: 'MARKSENTRY_ROLE',isChecked:true},
{id: '5', roleName: 'APPLICANT_ROLE',isChecked:false}
];
onChangeRole(userRole: string, isChecked) {
this.checkedInfo = isChecked;
for(var i=0;i<this.appUserRoleList.length;i++ ){
if( this.appUserRoleList[i].roleName==userRole){
this.appUserRoleList[i].isChecked=!this.appUserRoleList[i].isChecked;
}
}
}
checkedEvnt() {
for(var i=0;i<this.appUserRoleList.length;i++ ){
this.appUserRoleList[i].isChecked=false;
console.log(this.appUserRoleList[i].isChecked)
}
}
<h1>Unchek All Roles</h1>
<div class="form-check" *ngFor="let appUserRole of appUserRoleList">
<input class="form-check-input" name="{{appUserRole.roleName}}" type="checkbox" id="{{appUserRole.roleName}}" [checked]="appUserRole.isChecked" (change)="onChangeRole(appUserRole.roleName, $event)">
<label class="form-check-label" for="{{appUserRole.roleName}}">
{{appUserRole.roleName}}
</label>
</div>
<button (click)="checkedEvnt()">Uncheck All</button>
<pre>{{ userRoleListTemp | json}}</pre>
First mistake you made is you are adding (change)
event on button click. 您犯的第一个错误是单击按钮时添加
(change)
事件。 Replace it with (click)
as change event if for input type properties and you can only use it with ngModel . 如果是输入类型属性,则将其替换为
(click)
作为更改事件,并且只能将其与ngModel一起使用 。
You should add isChecked
property inside appUserRoleList
list, which will help you to check/uncheck checkbox. 您应该在
appUserRoleList
列表中添加isChecked
属性,这将帮助您选中/取消选中复选框。 Try this: 尝试这个:
appUserRoleList: any = [
{id: '1', roleName: 'SETUP_ROLE', isChecked: true},
{id: '2', roleName: 'ENTRY_ROLE', isChecked: false},
{id: '3', roleName: 'SEATPLAN_ROLE', isChecked: true},
{id: '4', roleName: 'MARKSENTRY_ROLE', isChecked: true},
{id: '5', roleName: 'APPLICANT_ROLE', isChecked: true}
];
On Uncheck All
button click loop though the appUserRoleList
and set isChecked = false
. 在“
Uncheck All
按钮上,单击循环通过appUserRoleList
并设置isChecked = false
。
Use a template variable to get a hold of the checkboxes: 使用模板变量来保留复选框:
<input #checkboxes class="form-check-input" name="{{appUserRole.roleName}}" type="checkbox" id="{{appUserRole.roleName}}" (change)="onChangeRole(appUserRole.roleName, $event)">
Then in your component using @ViewChild
get a hold of all the ElementRef
and process them by setting the checked
property to false
: 然后在使用
@ViewChild
的组件中,保留所有ElementRef
并通过将checked
属性设置为false
来处理它们:
@ViewChildren("checkboxes") checkboxes: QueryList<ElementRef>
checkedEvnt() {
this.checkboxes.forEach((element) => {
element.nativeElement.checked = false;
this.userRoleListTemp.length = 0;
});
}
This is your modified StackBlitz 这是您修改过的StackBlitz
why not use [(ngModel)]?? 为什么不使用[(ngModel)]? NO (change), NO split.
不(更改),不拆分。 Maintain the code simple.
保持代码简单。 See stackblitz
见stackblitz
<div class="form-check" *ngFor="let appUserRole of appUserRoleList">
<input #inputs class="form-check-input" name="{{appUserRole.roleName}}" type="checkbox" id="{{appUserRole.roleName}}" [(ngModel)]="appUserRole.checked">
<label class="form-check-label" for="{{appUserRole.roleName}}">
{{appUserRole.roleName}}
</label>
</div>
To get the array, I like a getter 为了得到数组,我喜欢吸气剂
get userRoleListTemp()
{
return this.appUserRoleList.filter(x=>x.checked).map(u=>+u.id)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.