Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 + Bootstrap 4 Select all and Deselect all Checkboxes

I'm having an issue trying to get Bootstrap 4 Checkboxes working with a select all and deselect all option in angular 6+. I can get it to work when I use the original code here: http://www.angulartutorial.net/2017/04/select-all-deselect-all-checkbox.html
But the issue is Bootstrap uses a different event to click their checkboxes. Does anyone have a solution for this?

<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>

And the 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() {
  }
}
like image 242
Robert Wojtow Avatar asked Aug 09 '18 23:08

Robert Wojtow


People also ask

How do I select all checkboxes?

In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.

How do you find checkbox is checked or not in angular 6?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.


1 Answers

this should do it

Here is a 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() {
  }
}
like image 88
racamp101 Avatar answered Sep 30 '22 19:09

racamp101