选中mat-radio-button时启用按钮

时间:2019-10-22 08:05:36

标签: angular typescript

我希望在选中mat-radio-button时启用按钮。但是,当我检查它时,它并非无法完成。我不知道问题出在哪里。以下我提供代码和演示供您参考。 DEMO供您参考

HTML

api_client

组件

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef [ngStyle]="{'display': 'none'}"> No. </th>
    <mat-cell *matCellDef="let row">
                    <mat-radio-button
                        (click)="$event.stopPropagation()" 
                        (change)="$event ? selection.select(row) : null"
                        [checked]="selection.isSelected(row)">
                    </mat-radio-button>
                </mat-cell>
  </ng-container>

  <ng-container matColumnDef="No">
    <th mat-header-cell *matHeaderCellDef [attr.rowspan]="2">No</th>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Symbol </th>
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

  <!-- Header row first group -->

  <!-- Header row second group -->
  <ng-container matColumnDef="header-row-second-group">
    <th mat-header-cell *matHeaderCellDef [attr.colspan]="2"> Second group </th>
  </ng-container>



  <tr mat-header-row *matHeaderRowDef="['No', 'header-row-second-group']"></tr>
  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

<button mat-raised-button color="accent" [disabled]="!selectedRow" (click)="deleteUser()">Delete User</button>

希望大家都能提供帮助

2 个答案:

答案 0 :(得分:2)

selectedRow错误时,删除按钮将被禁用。选中复选框后,您将需要一个函数来更新该变量。您需要在TableBasicExample组件中添加一个方法:

export class TableBasicExample {
  selectedRow: any;
  selection = new SelectionModel<PeriodicElement>(true, []);
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;

  deleteUser(){
    console.log(this.selectedRow)
  }

  checkBoxSelected(row: number) {
    this.selectedRow = row;
    console.log(this.selectedRow);
  }
}

然后相应地更新模板:

<ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef [ngStyle]="{'display': 'none'}"> No. </th>
    <mat-cell *matCellDef="let row">
      <mat-radio-button
        (click)="$event.stopPropagation()" 
        (change)="checkBoxSelected(row)" <!-- HERE is the change -->
        [checked]="selection.isSelected(row)">
      </mat-radio-button>
    </mat-cell>
  </ng-container>

Updated DEMO

答案 1 :(得分:2)

.html

 <mat-radio-button (click)="$event.stopPropagation()" 
                   (change)="$event ? selection.select(row) : null ;
                   callIt(row)"  <-- here is the change --->
                   [checked]="selection.isSelected(row)">
 </mat-radio-button>

.ts文件

export class TableBasicExample {
  selectedRow= false;
  selection = new SelectionModel<PeriodicElement>(true, []);
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;

 callIt(row){
    this.selectedRow = true
    console.log(row)
  }
  deleteUser(){
    console.log(this.selectedRow)
  }
}

DEMO here