首页 文章

有复选框的困难

提问于
浏览
0

有人能告诉我我做错了什么吗?

(角6)

https://stackblitz.com/edit/angular-jbqylr?file=src%2Fapp%2Fapp.component.html

我在我的 table 下面,在那里,在thead中,有一个主复选框,如果单击,则选中表格中的所有复选框 .

<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">
                <thead>
                    <tr>
                        <th><mat-checkbox class="example-margin" (change)="selectAllCb($event.checked)"></mat-checkbox></th>
                        <th>Nº Protocolo</th>
                        <th>Resumo</th>
                        <th>Interessado</th>
                        <th>Tipo</th>
                        <th>Data Cadastro</th>
                        <th>Ações</th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let documento of documentos, let i = index">
                        <td><mat-checkbox [ngModel]="selectAll"(change)="onChangeCheckBox($event.checked,documento?.id)"></mat-checkbox>
                            </td>
                        <td id="codigo">{{documento?.codigo}}/{{documento?.ano}}</td>
                        <td id="resumo">{{documento?.resumo}}</td>
                        <td id="interessado">{{documento?.interessado}}</td>
                        <td id="tipo">{{documento?.tipoDocumento?.nome}}</td>
                        <td id="data">{{documento?.dataCadastro | date: 'dd/MM/yyyy HH:mm'}}</td>
                        <td id="acoes">
                            <div class="row">
                                <button class="badge badge-light" [routerLink]="['/documentos/detalhe/',documento?.id]">
                                    <i class="material-icons" title="Editar protocolo">edit</i>
                                </button>
                                <button class="badge badge-light" (click)="gerarEtiqueta(documento?.id)">
                                    <i class="material-icons" title="Imprimir Etiqueta">print</i>
                                </button>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>

master复选框通过[ngModel]设置selectAll:boolean属性,该属性由表的其余部分的复选框使用 . ngfor生成的复选框在单击时将值传递给组件 .

export class DocumentosComponent implements OnInit, OnDestroy, AfterViewInit {
  selectAll: boolean = false;
constructor(){}

ngOnInit() {}

 selectAllCb(isChecked: boolean) { 
      this.selectAll = isChecked;

  }
}

但是,当属性绑定更改复选框的值时,不会触发更改事件 .

1 回答

  • 0

    可能是,$ event.checked返回undefined . 不要尝试操作员 .

    selectAllCb(state: boolean){
        console.log('called', state);
        this.selectAll = !this.selectAll;
    }
    

    如果那还不行 . 您可以手动拨打CD .

    constructor(private cd: ChangeDetectorRef){} 
    this.cd.markForCheck();
    

相关问题