简体   繁体   English

如何使用angular8中的反应形式选择和取消选择复选框

[英]how to select and deselect checkbox using reactive forms in angular8

Hi i have a array of objects with id and value, i am binding to html using reactive forms, when i click on select and deselect all, i dont get any errors nor get the required output.嗨,我有一组带有 id 和 value 的对象,我正在使用反应式表单绑定到 html,当我单击全选和取消全选时,我没有收到任何错误,也没有获得所需的输出。 I am not getting where my issue is.我不明白我的问题在哪里。

HTML: HTML:

<div class="col">

    <div class="row row-cols-3" formGroupName="Print">
        <div class="col" *ngFor="let print of PrintList;let i = index">
            <div class="custom-control custom-checkbox mb-3">
                <input type="checkbox" class="custom-control-input" id="{{print .id}}">
                <label class="custom-control-label" for="{{print.id}}">{{print.value}}</label>
            </div>
        </div>
        <div class="col">
            <div class="custom-control custom-checkbox mb-3">
                <input type="checkbox" class="custom-control-input" id="SelectDeselectAll" [checked]="isAllChecked()"
                    (change)="checkAll($event)">
                <label class="custom-control-label" for="SelectDeselectAll">Select/Deselect All</label>
            </div>
        </div>
    </div>
</div>

Ts: :

checkAll(ev) {
    this.PrintList.forEach(x => x.id = ev.target.checked)
}

isAllChecked() {
    return this.PrintList && this.PrintList.every(_ => _.id);
}

PrintList = [{ id: 1, value: "flowers" }, { id: 2, value: "fruits" }, { id: 1, value: "cars" }, { id: 1, value: "bikes" },]

Demo 演示

Try doing this :尝试这样做:

HTML : HTML :

<div class="col">

<div class="row row-cols-3" >
    <div class="col" *ngFor="let print of PrintList;let i = index">
        <div class="custom-control custom-checkbox mb-3">
            <input type="checkbox" class="custom-control-input" id="{{print .id}}" [checked]="print.checked">
            <label class="custom-control-label" for="{{print.id}}">{{print.value}}</label>
        </div>
    </div>
    <div class="col">
        <div class="custom-control custom-checkbox mb-3">
            <input type="checkbox" class="custom-control-input" id="SelectDeselectAll" [checked]="all"
                (change)="checkAll($event)">
            <label class="custom-control-label" for="SelectDeselectAll">Select/Deselect All</label>
        </div>
    </div>
</div>

TS : TS :

all = false;

checkAll(ev) {
if (!this.all) {
    this.PrintList.forEach(x => x.checked =  true)
    this.isAllChecked()
    } else {
    this.PrintList.forEach(x => x.checked =  false)
    this.isAllChecked()
 }
 }

 isAllChecked() {
   this.all = !this.all
 }

 PrintList = [
   { id: 1, checked: false, value: "flowers" },
   { id: 2, checked: false, value: "fruits" },
   { id: 1, checked: false, value: "cars" },
   { id: 1, checked: false, value: "bikes" },
  ]
 }

DEMO演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用反应形式使用angular8比较对象数组 - How to compare array of objects using angular8 using reactive forms 如何使用Angular2通过单击图像或图标来实现选择/取消选择复选框 - How to implement select/deselect by clicking images or icons instead checkbox using Angular2 使用angular8取消选中复选框时如何保持禁用输入 - How to keep input disabled when the checkbox is unchecked using angular8 Angular Reactive Forms:如何填充多选复选框表单? - Angular Reactive Forms: How to Populate A Multi-Selection Checkbox form? Angular 4:如何在反应形式中将字符串数组分配到复选框中 - Angular 4 : How to assign array of string into checkbox in reactive forms 使用jQuery的变量,多个复选框选择/取消选择 - A variable, Multiple Checkbox Select/Deselect using jQuery 如何在react js中选择/取消选择所有复选框? - How to select/deselect all checkbox in react js? 如何以Angular 7反应形式设置HTML选择元素的选定值? - How to set selected value of HTML Select Element in Angular 7 reactive forms? 如何在反应形式formarray angular中使用mat-select - How to use mat-select with reactive forms formarray angular 如何使用反应式 forms 设置预选下拉菜单? Angular - How to set pre-select dropdown with reactive forms? Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM