英文:
Why doesn't my Angular Material table update when the data source is changed?
问题
我使用mat-table来让用户查看应用程序的公司连接对象列表。
问题出现在用户点击编辑按钮并使用模态框更新公司连接时,表格没有反映出变化。
为了排除故障,我已经在多个地方将更新后的对象打印到控制台,并确认对象正在更新,似乎只是表格没有更新。
表格组件的HTML部分:
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z4">
<!-- 列定义 -->
<ng-container matColumnDef="DattoDomain">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Datto 域名 </th>
<td mat-cell *matCellDef="let company"> {{company.dattoDomain}} </td>
</ng-container>
<ng-container matColumnDef="connectWiseId">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ConnectWise ID </th>
<td mat-cell *matCellDef="let company"> {{company.connectWiseId}} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>操作</th>
<td mat-cell *matCellDef="let company">
<button mat-icon-button (click)="openEditCompanyModal(company)">
<mat-icon>edit</mat-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
表格组件的 TypeScript 部分:
export class CompanyTableComponent implements OnInit, AfterViewInit {
@ViewChild(MatSort) sort: MatSort;
@Input() companies: CompanyConnection[];
@Input() getError: boolean;
displayedColumns: string[] = ['DattoDomain', 'connectWiseId', 'actions'];
companies$: Observable<CompanyConnection[]> = of([]);
dataSource = new MatTableDataSource<CompanyConnection>();
constructor(
private companyDataService: CompanyDataService,
private dialog: MatDialog,
private changeDetectorRef: ChangeDetectorRef
) {}
ngOnInit() {
if (this.getError) {
this.companies$ = this.companyDataService.getErrorCompanyConnections();
} else {
this.companies$ = this.companyDataService.getHealthyCompanyConnections();
}
this.companies$.subscribe((companies: CompanyConnection[]) => {
this.dataSource.data = companies;
this.changeDetectorRef.detectChanges();
});
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
openEditCompanyModal(company: CompanyConnection) {
const dialogRef = this.dialog.open(CompanyModalComponent, {
data: { company }
});
dialogRef.afterClosed().subscribe((editedCompany: CompanyConnection) => {
if (editedCompany) {
// 更新现有的公司连接
this.companyDataService.editCompanyConnection(editedCompany).subscribe(
() => {},
error => {}
);
// 检索更新后的公司连接
this.companyDataService.getCompanyConnection(editedCompany.dattoDomain).subscribe(
(updatedCompany: CompanyConnection | undefined) => {
if (updatedCompany) {
console.log('更新后的公司连接:', updatedCompany);
} else {
console.log('未找到公司连接');
}
},
error => {
console.log('检索公司连接时出错:', error);
}
);
}
});
}
}
这个文件返回:更新后的公司连接:{dattoDomain: 'aptdynamics', connectWiseId: 'Bucks Communications', status: '200'}
这是来自 company-connection-service.service.ts
文件的一些方法。如果有其他能帮助的代码,请告诉我!
感谢!
英文:
I am using a mat-table so users can see a list of companyConnection objects for an application.
My issue occurs when a user clicks the edit button and updates a companyConnection using a modal, but the change does not reflect in the table.
For troubleshooting, I have printed the updated object to the console in multiple spots and have confirmed that the object is being updated, and it seems to just be a problem with the table not updating.
table.component.html
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z4">
<!-- Columns -->
<ng-container matColumnDef="DattoDomain">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Datto Domain </th>
<td mat-cell *matCellDef="let company"> {{company.dattoDomain}} </td>
</ng-container>
<ng-container matColumnDef="connectWiseId">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ConnectWise Id </th>
<td mat-cell *matCellDef="let company"> {{company.connectWiseId}} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>Actions</th>
<td mat-cell *matCellDef="let company">
<button mat-icon-button (click)="openEditCompanyModal(company)">
<mat-icon>edit</mat-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
export class CompanyTableComponent implements OnInit, AfterViewInit {
@ViewChild(MatSort) sort: MatSort;
@Input() companies: CompanyConnection[];
@Input() getError: boolean;
displayedColumns: string[] = ['DattoDomain', 'connectWiseId', 'actions'];
companies$: Observable<CompanyConnection[]> = of([]);
dataSource = new MatTableDataSource<CompanyConnection>();
constructor(
private companyDataService: CompanyDataService,
private dialog: MatDialog,
private changeDetectorRef: ChangeDetectorRef
) {}
ngOnInit() {
if (this.getError) {
this.companies$ = this.companyDataService.getErrorCompanyConnections();
} else {
this.companies$ = this.companyDataService.getHealthyCompanyConnections();
}
this.companies$.subscribe((companies: CompanyConnection[]) => {
this.dataSource.data = companies;
this.changeDetectorRef.detectChanges();
});
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
openEditCompanyModal(company: CompanyConnection) {
const dialogRef = this.dialog.open(CompanyModalComponent, {
data: { company }
});
dialogRef.afterClosed().subscribe((editedCompany: CompanyConnection) => {
if (editedCompany) {
// Update the existing company connection
this.companyDataService.editCompanyConnection(editedCompany).subscribe(
() => {},
error => {}
);
// Retrieve the updated company connection
this.companyDataService.getCompanyConnection(editedCompany.dattoDomain).subscribe(
(updatedCompany: CompanyConnection | undefined) => {
if (updatedCompany) {
console.log('Updated company connection:', updatedCompany);
} else {
console.log('Company connection not found');
}
},
error => {
console.log('Error retrieving company connection:', error);
}
);
}
});
}
}
this file returns: Updated company connection: {dattoDomain: 'aptdynamics', connectWiseId: 'Bucks Communications', status: '200'}
Here are methods from the company-connection-service.service.ts file
export class CompanyDataService {
private companies: CompanyConnection[] = [
{
dattoDomain: "aptdynamics",
connectWiseId: "Apartment Dynamics",
status: "200"
},
{
dattoDomain: "buckscomm",
connectWiseId: "Bucks Communications",
status: "200"
},
{
dattoDomain: "cardinalconcretecompany",
connectWiseId: "Cardinal Concrete Company",
status: "200"
},
{
dattoDomain: "centralcarolinaseeding",
connectWiseId: "Central Carolina Seeding",
status: "404"
},
];
private companiesSubject = new BehaviorSubject<CompanyConnection[]>(this.companies);
constructor() { }
getCompanyConnections(): Observable<CompanyConnection[]> {
return this.companiesSubject.asObservable();
}
getCompanyConnection(dattoDomain: string): Observable<CompanyConnection | undefined> {
return this.getCompanyConnections().pipe(
map((companies: CompanyConnection[]) => {
return companies.find((company: CompanyConnection) => {
return company.dattoDomain === dattoDomain;
});
})
);
}
getHealthyCompanyConnections(): Observable<CompanyConnection[]> {
return this.getCompanyConnections().pipe(
map((companies: CompanyConnection[]) => {
return companies.filter((company: CompanyConnection) => {
return company.status === "200";
});
})
);
}
getErrorCompanyConnections(): Observable<CompanyConnection[]> {
return this.getCompanyConnections().pipe(
map((companies: CompanyConnection[]) => {
return companies.filter((company: CompanyConnection) => {
return company.status !== "200";
});
})
);
}
addCompanyConnection(company: CompanyConnection): Observable<CompanyConnection[]> {
this.companies.push(company);
this.companiesSubject.next(this.companies);
return of(this.companies);
}
editCompanyConnection(updatedCompany: CompanyConnection): Observable<CompanyConnection[]> {
const index = this.companies.findIndex(c => c.dattoDomain === updatedCompany.dattoDomain);
if (index !== -1) {
this.companies[index] = updatedCompany;
}
return of(this.companies);
}
}
if there is any other code I can provide that would help, please let me know!
Thanks!
答案1
得分: 0
在更新后,在主题上调用next
。
editCompanyConnection(updatedCompany: CompanyConnection): Observable<CompanyConnection[]> {
const index = this.companies.findIndex(c => c.dattoDomain === updatedCompany.dattoDomain);
if (index !== -1) {
this.companies[index] = updatedCompany;
this.companiesSubject.next(this.companies); //添加这行
}
return of(this.companies);
}
英文:
Call next
on the subject after updating.
editCompanyConnection(updatedCompany: CompanyConnection): Observable<CompanyConnection[]> {
const index = this.companies.findIndex(c => c.dattoDomain === updatedCompany.dattoDomain);
if (index !== -1) {
this.companies[index] = updatedCompany;
this.companiesSubject.next(this.companies); //add this line
}
return of(this.companies);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论