使用NG风格的Notiffy用户已进行了更改



我有一个带有可编辑输入字段的垫子表。当进行更改以提醒用户节省这些更改时,我正在使用[sytle.color]将颜色更改为红色。但是,我只能一次将CSS更改为一行,并且正在完成整个行。发生更改时,将每个特定项目更改为红色的最佳方法是什么?这也是通知用户更改的最佳实践吗?这是完整的代码:https://stackblitz.com/edit/angular-aouc8q?file=App/table-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;
}
}

这是一个工作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

相关内容

最新更新