使用 NGStyle 通知用户已进行更改

Notiffy User with NG-Style that changes have been made

我有一个带 editable 输入字段的垫子 table。我正在使用 [sytle.color] 在进行更改时将颜色更改为红色以提醒用户保存这些更改。但是,我一次只能将 css 更改为一行,并且它正在执行整行。当发生变化时,将每个特定项目更改为红色的最佳方法是什么。这也是通知用户有关更改的最佳做法吗?这是完整的代码:https://stackblitz.com/edit/angular-aouc8q?file=app%2Ftable-selection-example.html

HTML

<button [disabled]="!changeMade" mat-raised-button color="primary" >Save Changes</button>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Checkbox Column -->
  <ng-container matColumnDef="select">
    <th mat-header-cell *matHeaderCellDef>
     test1
    </th>
    <td mat-cell *matCellDef="let row">
      <mat-checkbox (click)="$event.stopPropagation()"
                    (change)="$event ? selection.toggle(row) : null"
                    [checked]="position.isSelected(row)"
                    [aria-label]="checkboxLabel(row, 'one')">
      </mat-checkbox>
    </td>
  </ng-container>

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef>
     test 2
    </th>
    <td mat-cell *matCellDef="let row">
      <mat-checkbox (click)="$event.stopPropagation()"
                    (change)="$event ? selection.toggle(row) : null"
                    [checked]="position.isSelected(row)"
                    [aria-label]="checkboxLabel(row, 'two')">
      </mat-checkbox>
    </td>
  </ng-container>
  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element">
        <mat-form-field floatLabel="never">
          <input matInput 
          [style.color]="(changeMade && positionID == element.position) ? 'red': 'black'"
          [value]="element.name"
          [(ngModel)]="element.name" 
          (change)="edit(element)">
        </mat-form-field> </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element">
        <mat-form-field floatLabel="never">
          <input matInput 
          [value]="element.weight"
          [(ngModel)]="element.weight" 
          (change)="edit(element)"> 
          </mat-form-field> </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>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"
      (click)="selection.toggle(row)">
  </tr>
</table>

TS

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];


@Component({
  selector: 'table-selection-example',
  styleUrls: ['table-selection-example.css'],
  templateUrl: 'table-selection-example.html',
})
export class TableSelectionExample {
  displayedColumns: string[] = ['select', 'position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
  selection = new SelectionModel<PeriodicElement>(true, []);
  position = new SelectionModel<PeriodicElement>(true, []);
  changeMade: boolean = false;
  positionID: any;


  edit(element){
    this.positionID = element.position
    this.changeMade=true;
}
}

HERE IS A WORKING STACKBLITZ
我创建了一个数组 saved: boolean[][]; 来存储字段的状态,无论是否保存,当进行更改时,saved[changeRow][changeCol] = false;。然后对于颜色我看saved[element.position][i]。 这是我添加的内容:

ngOnInit(){
    this.saved = [];
    for(var i: number = 0; i < 11; i++) {
      this.saved[i] = [];
      for(var j: number = 0; j< 11; j++) {
        this.saved[i][j] = true;
      }
    }
  }
edit(element, col){
    this.saved[element.position][col]=false;
}

并将其添加到两列的 HTML 中:

[style.color]="saved[element.position][0] ? 'black': 'red'"
(change)="edit(element, 0)"

我还添加了一个保存按钮,我只是将 saved[][] 数组重置为所有 true