【问题标题】:Select all Checkboxes with single selection of checkbox in angular5 using angular material使用角度材料在angular5中选择所有复选框,单选复选框
【发布时间】:2018-08-25 04:08:58
【问题描述】:

我用 HTML 编写了这样的代码:

<table class="table tableWidths shadowTable">
      <thead>
        <tr class="tableHeaderBgColor">
          <th>
            <mat-checkbox (change)="$event ? masterToggle() : null" [checked]="selection.hasValue() && isAllSelected()" [indeterminate]="selection.hasValue() && !isAllSelected()">
            </mat-checkbox>
          </th>
          <th>Status</th>
          <th>Conflict ID</th>
          <th>Conflict Party</th>
          <th>Conflict Party ID</th>
          <th>Account Name</th>
          <th>Account Owner Name</th>
          <th>Account Owner ID</th>
          <th>Edit</th>
        </tr>
      </thead>
      <tbody>
        <ng-container *ngFor="let conf of conflicts;let ns=index;let odd=odd;">
          <tr [class.tableOdd]="odd">
            <td>
              <mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? selection.toggle(conf) : null" [checked]="selection.isSelected(conf)">
              </mat-checkbox>
            </td>
            <td>{{conf.Status}}</td>
            <td>
              {{conf.Conflict_ID}}
            </td>
            <td>{{conf.Conflict_Party}}</td>
            <td>{{conf.Conflict_Party_ID}}</td>
            <td>{{conf.Account_Name}}</td>
            <td>{{conf.Account_Owner_Name}}</td>
            <td>{{conf.Account_Owner_ID}}</td>
            <td>
              <mat-icon>edit</mat-icon>
            </td>
          </tr>
        </ng-container>
      </tbody>
    </table>

在.ts文件中的代码是这样写的:

conflicts = [
    {
      Status: 'Pending',
      Conflict_ID: 203888400,
      Conflict_Party: 'Collier County Pulish',
      Conflict_Party_ID: 12345,
      Account_Name: 'Asian Group LD',
      Account_Owner_Name: 'Mike',
      Account_Owner_ID: 12345, checked: false
    },
    {
      Status: 'Approved',
      Conflict_ID: 203888400,
      Conflict_Party: 'Collier County Pulish',
      Conflict_Party_ID: 12345,
      Account_Name: 'Asian Group LD',
      Account_Owner_Name: 'Mary',
      Account_Owner_ID: 12345, checked: false
    }
  ]    
isAllSelected() {
        const numSelected = this.selection.selected.length;
        const numRows = this.conflicts.length;
        return numSelected === numRows;
      }

      masterToggle() {
        this.isAllSelected() ?
          this.selection.clear() :
          this.conflicts.forEach(row => this.selection.select());
      }
      isSelected(item) {
        console.log('item ' + JSON.stringify(item));
      }

在我的数组中,冲突 ID 是获得进一步处理的值。 Whenever selection done get the key value from this key 'Conflict_ID'. 这里无法在表中的单选中选择所有复选框。 我可以取消选择所有复选框。 任何人都可以为此提出一些解决方案。

【问题讨论】:

  • 如果我错了,请纠正我。您想要的是,当您选择主复选框时,应选中所有复选框,反之亦然。
  • 您没有显示selection.hasValue()selection.toggle() 的代码或selection 对象的结构。
  • 另外 - 您正在绑定 [checked]="selection.isSelected(conf)",但该函数没有返回语句。

标签: angular checkbox angular-material


【解决方案1】:

我已经为 Angular 材质复选框制作了一个示例,其中包含全选选项。请参阅Working version in stackblitz。希望对你有帮助

组件.ts

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

/**
 * @title Configurable checkbox
 */
@Component({
  selector: 'checkbox-configurable-example',
  templateUrl: 'checkbox-configurable-example.html',
  styleUrls: ['checkbox-configurable-example.css'],
})
export class CheckboxConfigurableExample {
  pizzaIng : any;
  selectAll = false;

  constructor(){
    this.pizzaIng=[
      {name : "Pepperoni", checked : false},
      {name : "Sasuage", checked : true},
      {name : "Mushrooms", checked : false}
    ];
  }

  updateCheck(){
    console.log(this.selectAll);
    if(this.selectAll === true){
      this.pizzaIng.map((pizza)=>{
        pizza.checked=true;
      });

    }else {
      this.pizzaIng.map((pizza)=>{
        pizza.checked=false;
      });
    }
  }
}

HTML 文件

<mat-card class="result">
  <mat-card-content>
    <h2 class="example-h2">Sample</h2>

    <section *ngFor="let ing of pizzaIng; let i = index" class="example-section">
      <mat-checkbox
         [(ngModel)]="ing.checked">
        {{ing.name}}
      </mat-checkbox>
    </section>
  </mat-card-content>
</mat-card>


<mat-card class="result">
  <mat-card-content>
    <h2 class="example-h2">Select / Deselect</h2>

    <section class="example-section">
      <mat-checkbox (change)="updateCheck()"
          class="example-margin"
          [(ngModel)]="selectAll">
        Select All
      </mat-checkbox>
    </section>
  </mat-card-content>
</mat-card>

【讨论】:

  • 感谢您的回复。这里数组包含“已检查”键,但是在我的数组中没有可用性。
  • 您的阵列会是什么样子? selection.hasValue() 未在您的共享代码中定义。在 stackblitz 中分享完整的代码或工作版本。尝试以 JSON 格式添加检查键,否则您的代码将变得更加复杂。
  • 在“.ts”文件代码中编辑了我的问题,请检查。检查任意复选框时,将键的值作为“冲突”。 span>
  • 不工作,而不是在我的 JSON 中添加检查键,有没有其他方法可以解决这个问题。我在问这个。
  • 使用 ngModel 并喜欢共享的示例。它会工作
猜你喜欢
  • 2020-07-13
  • 2019-06-02
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2018-03-31
  • 2022-11-28
  • 1970-01-01
相关资源
最近更新 更多