Dheeraj Agrawal
Dheeraj Agrawal

Reputation: 2357

Make select/unselect all functionality in angular 2

I am new to angular 2 & trying to create select/unselect all checkboxes. I have a table like this:

<table>
    <thead>
        <tr>
            <th><input type="checkbox" name="selectAll"></th>
            <th>Name</th>
            <th>Number</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox"></td>
            <td>abc</td>
            <td>9876373773</td>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td>abc</td>
            <td>9876373773</td>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td>abc</td>
            <td>9876373773</td>
        </tr>
    </tbody>
</table>

So if user clicks on selectAll checkbox other checkboxes should get check to. Data is coming dynamically, here is my api call:

    this.contactService.getName(this.pageNumber, this.pageSize)
      .subscribe(
        data => { 
          this.contactlist = data.results;  
        },
        err => console.error(err),
        () => console.log('Contact list fetched')
      );

Upvotes: 1

Views: 2396

Answers (2)

dfsq
dfsq

Reputation: 193291

With data-driven table you could achieve this by updating row-model's "checked" flag. For example you could do something like this:

<table>
    <thead>
        <tr>
            <th><input type="checkbox" name="selectAll" [checked]="toggle" (change)="toggleAll()"></th>
            <th>Name</th>
            <th>Number</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of items">
            <td><input type="checkbox" [checked]="item.checked" (change)="toggleItem(item)"></td>
            <td>{{ item.name }}</td>
            <td>{{ item.number }}</td>
        </tr>
    </tbody>
</table>

And then in component:

toggle = false

items = [
  { name: 'aaa', number: '3452342343' },
  { name: 'aaa', number: '3452342343' }
]

toggleItem(item) {
  item.checked = !item.checked
  this.toggle = this.items.every(item => item.checked)
}

toggleAll() {
  this.toggle = !this.toggle
  this.items.forEach(item => item.checked = this.toggle)
}

Note that this is important that you also handle cases when you check all checkboxes manually and it should automatically check checkAll checkbox. And vise versa.

Demo: http://plnkr.co/edit/Nw8Wk0kXSbQLkAE7yW0e?p=info

Upvotes: 1

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657741

In Angular2 the preferred way is to bind HTML to a model, update the model and let Angular update the DOM like:

@Component({
  selector: 'my-comp',
  template: `
<table>
    <thead>
        <tr>
            <th><input type="checkbox" name="selectAll" (click)="toggleAll($event.target.checked)"></th>
            <th>Name</th>
            <th>Number</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let item of contactlists let i=index">
            <td><input type="checkbox" [(ngModel)]="checkedItems[i]"></td>
            <td>{{item.name}}</td>
            <td>{{item.value}}</td>
        </tr>
    </tbody>
</table>
`)}
export class MyComponent {
  constructor() {
    this.contactService.getName(this.pageNumber, this.pageSize)
      .subscribe(
        data => { 
          this.contactlist = data.results;  
          this.checkedItems = new Array(this.contactlist?.length).fill(false);
        },
        err => console.error(err),
        () => console.log('Contact list fetched')
      );
  }
  /* data set dynamically from `contactService` instead according to updated question
  items = [
    {name: abc, number: 9876373771, checked: false},
    {name: def, number: 9876373772, checked: false},
    {name: ghi, number: 9876373773, checked: false},
  ]; */

  toggleAll(checked) {
    this.items.forEach((item) => item.checked = checked);
  }
}

Upvotes: 1

Related Questions