admin管理员组文章数量:1130349
I am new to RxResource API with angular 19. The issue is that the material table doesn't display data loading from rxResource (but there is a little trick that I can make to display it. It doesn't make sense at all the way I did). Here is my code: my template:
<mat-table #table [dataSource]="dataSource" matSort class="mat-elevation-z8" multiTemplateDataRows>
<ng-container matColumnDef="webTubeLink">
<mat-header-cell *matHeaderCellDef mat-sort-header>Tube Link</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeLink}}</mat-cell>
</ng-container>
<ng-container matColumnDef="videoId">
<mat-header-cell *matHeaderCellDef mat-sort-header>Video Id</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.videoId}}</mat-cell>
</ng-container>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header>Title</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeTitle}}</mat-cell>
</ng-container>
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
<span><i class='bi bi-plus-circle' (click)="addNew()"></i> Add</span>
</mat-header-cell>
<mat-cell *matCellDef="let row; let i=index;">
<span class='bi bi-pencil-fill' (click)="startEdit(row)" style="margin-right: 1rem"> Edit</span>
<span class='bi bi-trash-fill' (click)="deleteItem(row)"> Delete</span>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>
here is my code component:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
tubeRecords = computed(() => {
this.dataSource = new MatTableDataSource(this.tubeRecordsRS.value());
this.dataSource.paginator = this.paginator;
return this.tubeRecordsRS.value();
})
Here is my fix to display data in material table by adding the code below to the template
<p style="display: none">{{ tubeRecords() | json }}</p>
with the code the app is working as it supposed to.
Why? What did I do wrong? Is there any other way to fix this issue?
I am new to RxResource API with angular 19. The issue is that the material table doesn't display data loading from rxResource (but there is a little trick that I can make to display it. It doesn't make sense at all the way I did). Here is my code: my template:
<mat-table #table [dataSource]="dataSource" matSort class="mat-elevation-z8" multiTemplateDataRows>
<ng-container matColumnDef="webTubeLink">
<mat-header-cell *matHeaderCellDef mat-sort-header>Tube Link</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeLink}}</mat-cell>
</ng-container>
<ng-container matColumnDef="videoId">
<mat-header-cell *matHeaderCellDef mat-sort-header>Video Id</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.videoId}}</mat-cell>
</ng-container>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header>Title</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeTitle}}</mat-cell>
</ng-container>
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
<span><i class='bi bi-plus-circle' (click)="addNew()"></i> Add</span>
</mat-header-cell>
<mat-cell *matCellDef="let row; let i=index;">
<span class='bi bi-pencil-fill' (click)="startEdit(row)" style="margin-right: 1rem"> Edit</span>
<span class='bi bi-trash-fill' (click)="deleteItem(row)"> Delete</span>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>
here is my code component:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
tubeRecords = computed(() => {
this.dataSource = new MatTableDataSource(this.tubeRecordsRS.value());
this.dataSource.paginator = this.paginator;
return this.tubeRecordsRS.value();
})
Here is my fix to display data in material table by adding the code below to the template
<p style="display: none">{{ tubeRecords() | json }}</p>
with the code the app is working as it supposed to.
Why? What did I do wrong? Is there any other way to fix this issue?
Share Improve this question edited Jan 26 at 21:53 JSON Derulo 17.5k11 gold badges56 silver badges73 bronze badges asked Jan 26 at 20:21 D052057D052057 1331 silver badge10 bronze badges2 Answers
Reset to default 1This can be classified as derived state, we can achieve it in two ways:
Encapsulate inside the rxResource loader using rxjs operators:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes').pipe(
map((response: any) => {
const dataSource = new MatTableDataSource(response);
dataSource.paginator = this.paginator;
return dataSource;
})
)
});
HTML:
<mat-table #table [dataSource]="tubeRecordsRS.value()" matSort class="mat-elevation-z8" multiTemplateDataRows>
...
Use a computed to get the data source:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
tubeRecords = computed(() => {
const dataSource = new MatTableDataSource(this.tubeRecordsRS.value());
dataSource.paginator = this.paginator;
return dataSource;
})
HTML:
<mat-table #table [dataSource]="tubeRecords()" matSort class="mat-elevation-z8" multiTemplateDataRows>
...
As you can read in the docs, the computation inside the computed function is only executed if the signal value is actually read, which is not the case in your app, unless the paragraph is added which includes the signal read. Which means that your data source is never constructed.
A better way to connect your data source with the resource's data is via an effect:
export class YourComponent implements AfterViewInit {
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
dataSource = new MatTableDataSource();
constructor() {
effect(() => {
this.dataSource.data = this.tubeRecordsRS.value();
});
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
}
I am new to RxResource API with angular 19. The issue is that the material table doesn't display data loading from rxResource (but there is a little trick that I can make to display it. It doesn't make sense at all the way I did). Here is my code: my template:
<mat-table #table [dataSource]="dataSource" matSort class="mat-elevation-z8" multiTemplateDataRows>
<ng-container matColumnDef="webTubeLink">
<mat-header-cell *matHeaderCellDef mat-sort-header>Tube Link</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeLink}}</mat-cell>
</ng-container>
<ng-container matColumnDef="videoId">
<mat-header-cell *matHeaderCellDef mat-sort-header>Video Id</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.videoId}}</mat-cell>
</ng-container>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header>Title</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeTitle}}</mat-cell>
</ng-container>
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
<span><i class='bi bi-plus-circle' (click)="addNew()"></i> Add</span>
</mat-header-cell>
<mat-cell *matCellDef="let row; let i=index;">
<span class='bi bi-pencil-fill' (click)="startEdit(row)" style="margin-right: 1rem"> Edit</span>
<span class='bi bi-trash-fill' (click)="deleteItem(row)"> Delete</span>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>
here is my code component:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
tubeRecords = computed(() => {
this.dataSource = new MatTableDataSource(this.tubeRecordsRS.value());
this.dataSource.paginator = this.paginator;
return this.tubeRecordsRS.value();
})
Here is my fix to display data in material table by adding the code below to the template
<p style="display: none">{{ tubeRecords() | json }}</p>
with the code the app is working as it supposed to.
Why? What did I do wrong? Is there any other way to fix this issue?
I am new to RxResource API with angular 19. The issue is that the material table doesn't display data loading from rxResource (but there is a little trick that I can make to display it. It doesn't make sense at all the way I did). Here is my code: my template:
<mat-table #table [dataSource]="dataSource" matSort class="mat-elevation-z8" multiTemplateDataRows>
<ng-container matColumnDef="webTubeLink">
<mat-header-cell *matHeaderCellDef mat-sort-header>Tube Link</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeLink}}</mat-cell>
</ng-container>
<ng-container matColumnDef="videoId">
<mat-header-cell *matHeaderCellDef mat-sort-header>Video Id</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.videoId}}</mat-cell>
</ng-container>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef mat-sort-header>Title</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.webTubeTitle}}</mat-cell>
</ng-container>
<ng-container matColumnDef="actions">
<mat-header-cell *matHeaderCellDef>
<span><i class='bi bi-plus-circle' (click)="addNew()"></i> Add</span>
</mat-header-cell>
<mat-cell *matCellDef="let row; let i=index;">
<span class='bi bi-pencil-fill' (click)="startEdit(row)" style="margin-right: 1rem"> Edit</span>
<span class='bi bi-trash-fill' (click)="deleteItem(row)"> Delete</span>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>
here is my code component:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
tubeRecords = computed(() => {
this.dataSource = new MatTableDataSource(this.tubeRecordsRS.value());
this.dataSource.paginator = this.paginator;
return this.tubeRecordsRS.value();
})
Here is my fix to display data in material table by adding the code below to the template
<p style="display: none">{{ tubeRecords() | json }}</p>
with the code the app is working as it supposed to.
Why? What did I do wrong? Is there any other way to fix this issue?
Share Improve this question edited Jan 26 at 21:53 JSON Derulo 17.5k11 gold badges56 silver badges73 bronze badges asked Jan 26 at 20:21 D052057D052057 1331 silver badge10 bronze badges2 Answers
Reset to default 1This can be classified as derived state, we can achieve it in two ways:
Encapsulate inside the rxResource loader using rxjs operators:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes').pipe(
map((response: any) => {
const dataSource = new MatTableDataSource(response);
dataSource.paginator = this.paginator;
return dataSource;
})
)
});
HTML:
<mat-table #table [dataSource]="tubeRecordsRS.value()" matSort class="mat-elevation-z8" multiTemplateDataRows>
...
Use a computed to get the data source:
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
tubeRecords = computed(() => {
const dataSource = new MatTableDataSource(this.tubeRecordsRS.value());
dataSource.paginator = this.paginator;
return dataSource;
})
HTML:
<mat-table #table [dataSource]="tubeRecords()" matSort class="mat-elevation-z8" multiTemplateDataRows>
...
As you can read in the docs, the computation inside the computed function is only executed if the signal value is actually read, which is not the case in your app, unless the paragraph is added which includes the signal read. Which means that your data source is never constructed.
A better way to connect your data source with the resource's data is via an effect:
export class YourComponent implements AfterViewInit {
tubeRecordsRS = rxResource<any[], any>({
loader: () => this.http.get<any[]>('/Tube/Gettubes')
});
dataSource = new MatTableDataSource();
constructor() {
effect(() => {
this.dataSource.data = this.tubeRecordsRS.value();
});
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
}
本文标签: angularRxResourcesignalMatTable issuestemplate show no dataStack Overflow
版权声明:本文标题:angular - RxResource, Signal, MatTable issues - template show no data - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1738296846a1559195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论