angular - 如何将 Angular Material Table 数据刷新回页面中的初始状态?
问题描述
一些注意事项:
- 我的目标是实现一些“丢弃”和“保存”更改类型的功能。
- “companyService”服务调用一个从数据库中获取前“X”家公司(在本例中为 5 家)的 api。
- 该表通过 DataSource 实例填充。
- 该应用程序最终将投入生产。
因此,用户将能够切换开/关按钮并从下拉菜单中选择“允许”或“拒绝”。在用户与这些按钮交互时,我想在保存按钮旁边显示一些文本,说明有未保存的更改。但我似乎无法弄清楚的部分是如何处理“放弃更改”按钮上的单击事件,并在用户进行一些更改后将表中的数据“重置”回其初始状态。我只是感到困惑,因为表的数据源与服务器上的数据库相关联。
如果您能给我有关使用 DataSource 的具体建议,或者只是指出我正确的方向,将不胜感激!
[![][1]][1]
我的组件类引用此 CompanyDataSource 类来填充表。
代码
<table mat-table #companyTable [trackBy]="trackByIndex" [dataSource]="dataSource" matSort class="mat-elevation-z8">
<!-- Client ID Column -->
<ng-container matColumnDef="clientId">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Client ID </th>
<td mat-cell *matCellDef="let company">{{ company.clientId }}</td>
<td mat-footer-cell *matFooterCellDef>
<button mat-button (click)="onDiscardChanges()">Discard Changes</button>
</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Company </th>
<td mat-cell *matCellDef="let company"> {{ company.name }}</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<!-- Include Bsa Details Column -->
<ng-container matColumnDef="includeBsaDetails">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Include BSA Details </th>
<td mat-cell *matCellDef="let company">
<mat-button-toggle-group #toggleGroup="matButtonToggleGroup">
<mat-button-toggle class="on" [checked]="company.includeBsaDetails" [value]="true" (change)="onIncludeBsaDetailsButtonToggleEvent($event, company)">ON</mat-button-toggle>
<mat-button-toggle class="off" [checked]="!company.includeBsaDetails" [value]="false" (change)="onIncludeBsaDetailsButtonToggleEvent($event, company)">OFF</mat-button-toggle>
</mat-button-toggle-group>
</td>
<td mat-footer-cell *matFooterCellDef></td>
</ng-container>
<!-- Bsa Tool Permission Column -->
<ng-container matColumnDef="bsaToolPermission">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Implicit BSA Permission </th>
<td mat-cell *matCellDef="let company">
<mat-form-field>
<mat-select [(value)]="company.bsaToolPermission" (change)="onBsaToolPermissionSelectChangeEvent($event, company)">
<mat-option value="ALLOW">ALLOW</mat-option>
<mat-option value="DENY">DENY</mat-option>
</mat-select>
</mat-form-field>
</td>
<td mat-footer-cell *matFooterCellDef>
</td>
</ng-container>
<!-- Auto Lock Confirmation Column -->
<ng-container matColumnDef="autoLockConfirm">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Automated Lock Confirmation </th>
<td mat-cell *matCellDef="let company">
<mat-button-toggle-group #toggleGroup2="matButtonToggleGroup">
<mat-button-toggle class="on" [checked]="company.autoLockConfirm" [value]="true" (change)="onAutoLockConfirmButtonToggleEvent($event, company)">ON</mat-button-toggle>
<mat-button-toggle class="off" [checked]="!company.autoLockConfirm" [value]="false" (change)="onAutoLockConfirmButtonToggleEvent($event, company)">OFF</mat-button-toggle>
</mat-button-toggle-group>
</td>
<td mat-footer-cell *matFooterCellDef>
<button mat-button>Save Changes</button>
</td>
</ng-container>
<!-- Table Configuration for rows, header, and footer -->
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
<tr mat-footer-row *matFooterRowDef="columnsToDisplay"></tr>
</table>
解决方案
你不能打电话loadCompanies()
从服务器重新获取数据吗?那应该将表替换为数据库具有的值。
如果由于某种原因这不起作用,另一个想法是跟踪原始数据,然后将网格重置为原始数据。
class CompanyDataSource extends DataSource<any> {
public companiesSubject = new BehaviorSubject<Company[]>([]);
public originalData: Company[];
constructor(private companyService: CompanyService) {
super();
}
connect(): Observable<Company[]> {
return this.companiesSubject.asObservable();
}
disconnect() {
this.companiesSubject.complete();
}
loadCompanies(amount: number) {
this.companyService.GetTopXCompanies(amount).subscribe((companies) => {
this.originalData = companies;
const deepCopyData = JSON.parse(JSON.stringify(companies));
this.companiesSubject.next(deepCopyData);
});
}
discardChanges() {
const deepCopyData = JSON.parse(JSON.stringify(this.originalData));
this.companiesSubject.next(deepCopyData);
}
}
推荐阅读
- java - 灰熊 PRESERVE_HEADER_CASE
- javascript - 附加时节点从循环中消失
- angular - 在 Angular 7 中,如何访问发出事件的组件?
- c# - MVC 登录表单提交挂起而不点击代码
- go - 在 ElasticSearch 上使用特定搜索类型进行分页
- flutter - 颤动Transform.translate动画从和到小部件的中心
- c# - 当尝试从 AppointmentItem 获取 GlobalAppointmentId 时,它有时会返回 null
- swift - SwiftUI Conditional List - Unable to infer complex closure return type
- python - emacs flycheck-mode python3 print 语句,文件=sys.stderr
- asp.net - 两个表之间的匹配记录