gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:37:35 25 4
gpt4 key购买 nike

我有一个带有可编辑输入字段的垫表。当进行更改时,我使用 [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

关于javascript - 使用 NGStyle 通知用户已进行更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56221255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com