0

I have a mat table with editable input fields. I am using [sytle.color] to change the color to red when a change has been made to remind the user to save those changes. However I am only able to change the css to one row at a time and it is doing the whole row. What is the best way to change each specific item to red when there is a change. Also is this the best practice to notify the user about changes? Here is the full code: 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;
}
}
Flash
  • 924
  • 3
  • 22
  • 44

1 Answers1

1

HERE IS A WORKING STACKBLITZ
I created an array saved: boolean[][]; that stores the state of the field, saved or not, when a change is made, saved[changeRow][changeCol] = false;. Then for the color I look at saved[element.position][i]. Here's what I added:

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

and added this to the HTML of both columns:

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

I also added a button to save, where I just reset the saved[][] array back to all true

Ala Abid
  • 2,256
  • 23
  • 35