选择/取消选择角度2中的所有功能

时间:2016-07-04 07:38:34

标签: angularjs angular

我是 angular 2 &的新手尝试创建选择/取消选中所有复选框。我有一张这样的桌子:

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

因此,如果用户点击selectAll复选框,则应检查其他复选框。数据是动态的,这是我的api电话:

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

2 个答案:

答案 0 :(得分:1)

在Angular2中,首选方法是将HTML绑定到模型,更新模型并让Angular更新DOM,如:

@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);
  }
}

答案 1 :(得分:1)

使用数据驱动表,您可以通过更新行模型的“已检查”标志来实现此目的。例如,您可以这样做:

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

然后在组件中:

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

请注意,当您手动选中所有复选框并且应自动选中checkAll复选框时,这也很重要。反之亦然。

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