Angular 6 + Bootstrap 4全选并取消全选复选框

时间:2018-08-09 23:07:16

标签: javascript html bootstrap-4 angular6

我在尝试使Bootstrap 4 Checkboxs与select all一起使用并在angular 6+中取消选择all选项时遇到问题。当我在这里使用原始代码时,我可以使它工作: http://www.angulartutorial.net/2017/04/select-all-deselect-all-checkbox.html
但是问题是,Bootstrap使用另一个事件来单击其复选框。有人对此有解决方案吗?

<div class="form-check">
    <input class="form-check-input" type="checkbox" (change)="selectAll()">
    <label class="form-check-label">
        Select All
      </label>
</div>
<div class="form-check" *ngFor="let n of names">
    <input class="form-check-input" type="checkbox" value="{{n.name}}" [(ngModel)]="selectedNames" (change)="checkIfAllSelected()">
    <label class="form-check-label">
        {{n.name}}
      </label>
</div>

TS:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-checkbox',
  templateUrl: './checkbox.component.html',
  styleUrls: ['./checkbox.component.scss']
})
export class CheckboxComponent implements OnInit {
  title = 'Checkbox';
  names: any;
  selectedAll: any;
  constructor() {
    this.title = "Select all/Deselect all checkbox - Angular 2";
    this.names = [
      { name: 'Prashobh', selected: false },
      { name: 'Abraham', selected: false },
      { name: 'Anil', selected: false },
      { name: 'Sam', selected: false },
      { name: 'Natasha', selected: false },
      { name: 'Marry', selected: false },
      { name: 'Zian', selected: false },
      { name: 'karan', selected: false },
    ]

  }
  selectAll() {
    for (var i = 0; i < this.names.length; i++) {
      this.names[i].selected = this.selectedAll;
    }
  }
  checkIfAllSelected() {
    this.selectedAll = this.names.every(function(item:any) {
        return item.selected == true;
      })
  }

  ngOnInit() {
  }
}

2 个答案:

答案 0 :(得分:3)

这应该做到

这里是一个plnkr:https://next.plnkr.co/edit/ypGmwE32Xn1bgbqd?preview

HTML:

<div class="form-check">
    <input class="form-check-input" type="checkbox" (change)="selectAll()" [checked]="selectedAll">
    <label class="form-check-label">
        Select All
      </label>
</div>
<div class="form-check" *ngFor="let n of names">
    <input class="form-check-input" type="checkbox" value="{{n.name}}" [(ngModel)]="n.selected" (change)="checkIfAllSelected()">
    <label class="form-check-label">
        {{n.name}}
      </label>
</div>

TS:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-checkbox',
  templateUrl: './checkbox.component.html',
  styleUrls: ['./checkbox.component.scss']
})
export class CheckboxComponent implements OnInit {


    title = 'Checkbox';
  names: any;
  selectedAll: any;
  selectedNames: any;
    constructor() {
          this.title = "Select all/Deselect all checkbox - Angular 2";
    this.names = [
      { name: 'Prashobh', selected: false },
      { name: 'Abraham', selected: false },
      { name: 'Anil', selected: false },
      { name: 'Sam', selected: false },
      { name: 'Natasha', selected: false },
      { name: 'Marry', selected: false },
      { name: 'Zian', selected: false },
      { name: 'karan', selected: false },
    ]
    }
    selectAll() {
        this.selectedAll = !this.selectedAll;

        for (var i = 0; i < this.names.length; i++) {
            this.names[i].selected = this.selectedAll;
        } 
  }
  checkIfAllSelected() {
      var totalSelected =  0;
      for (var i = 0; i < this.names.length; i++) {
            if(this.names[i].selected) totalSelected++;
        } 
    this.selectedAll = totalSelected === this.names.length;

  return true;
  }

  ngOnInit() {
  }
}

答案 1 :(得分:0)

我不确定引导程序是否具有复选框中用于事件绑定的任何特殊逻辑。我想您的代码中缺少该代码。有关绑定的信息,请参见以下代码段:

<form [formGroup]="form">
    <div class="form-check" formArrayName="unitArr">
                        <input class="form-check-input" type="checkbox" [checked]="checkAllSelected()" 
         (click)="selectAll($event.target.checked)" id="select-all">
                        <label class="form-check-label" for="select-all">
                            Select All
                        </label>
                    </div>
                    <div class="form-check" *ngFor="let n of names; let i = index;" >
                        <input class="form-check-input" type="checkbox" value="{{n.name}}" [(ngModel)]="selectedNames" (change)="checkIfAllSelected()" [attr.id]="'check' + i">
                        <label class="form-check-label" [attr.for]="'check' + i">
                            {{n.name}}
                        </label>
                    </div>
</form>

checkAllSelected() {
  return this.form.controls.unitArr.controls.every(x => x.value == true)
}

selectAll(isChecked) {
  if isChecked 
     this.form.controls.unitArr.controls.map(x => x.patchValue(true))
  else
     this.form.controls.unitArr.controls.map(x => x.patchValue(false))
}

输入的“ id”必须映射为“ for”的“ label”。不知道您是否有任何特殊要求,但这是非常基本的HTML概念。请尝试一下,看看问题是否解决。

编辑:请注意,我不熟悉Angular6的概念,但是以上代码确实适用于Angular2。您可以检查上述几点是否对您有帮助。祝一切顺利!