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

时间:2019-05-20 12:41:16

标签: javascript angular angular-material mat-table

我有一个带有可编辑输入字段的垫子表。进行更改时,我正在使用<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> 将颜色更改为红色,以提醒用户保存这些更改。但是我只能一次将css更改为一行,并且它正在整行执行。有更改时,将每个特定项目更改为红色的最佳方法是什么。这也是通知用户有关更改的最佳做法吗?这是完整的代码:https://stackblitz.com/edit/angular-aouc8q?file=app%2Ftable-selection-example.html

HTML

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;
}
}

TS

{{1}}

1 个答案:

答案 0 :(得分:1)

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