angular - Mat-Table 排序标题未呈现
问题描述
我正在尝试实现一个带有可排序标题的材料表,在遵循教程之后,我有以下内容:
组件文件:
<mat-table [dataSource]="quotes" matSort matSortActive="idCpQuote" matSortDirection="asc" matSortDisableClear class="mat-elevation-z8">
<ng-container matColumnDef="idCpQuote">
<mat-header-cell *matHeaderCellDef mat-sort-header> ID. </mat-header-cell>
<mat-cell *matCellDef="let quote"> {{quote.idCpQuote}} </mat-cell>
</ng-container>
</mat-table>
<mat-paginator [length]="total" [pageSize]="20" [pageSizeOptions]="[20, 50, 100]"></mat-paginator>
在 component.ts 文件中:
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { QuoteService } from '@app/services/quote.service';
import { first, tap} from 'rxjs/operators';
import { QuoteDataSource } from '@app/services/quote.datasource';
import { QuoteResolver } from '@app/services/quote.resolver';
import { MatPaginator } from '@angular/material/paginator';
import { ActivatedRoute } from '@angular/router';
import { QuoteListMeta } from '@app/models/quote.list.meta';
@Component({
selector: 'app-quotes-list',
templateUrl: './quotes-list.component.html',
styleUrls: ['./quotes-list.component.scss'],
providers: [ QuoteService ]
})
export class QuotesListComponent implements AfterViewInit, OnInit {
quoteListMeta: QuoteListMeta;
quotes = null;
displayedColumns = ['idCpQuote', 'name', 'email', 'dateAdd'];
dataSource: QuoteService;
total: number;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private route: ActivatedRoute, private quoteService: QuoteService) {
}
ngOnInit(): void {
this.route.data.subscribe((data) => {
this.total = data.quoteListMeta.total;
});
this.dataSource = this.quoteService;
this.dataSource.getQuotes('', 'asc', 0, 20).subscribe(
response => {
this.quotes = response;
}
);
}
ngAfterViewInit() {
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
this.paginator.page
.pipe(
tap(() => this.loadQuotesPage())
)
.subscribe();
}
loadQuotesPage() {
this.dataSource.getQuotes('', 'asc', this.paginator.pageIndex, this.paginator.pageSize).subscribe(
response => {
this.quotes = response;
}
);
}
}
材料模块已经导入到我的 app.module.ts 中。
该表按预期呈现,分页也是如此,但是排序箭头根本不呈现。
我只是看不出我做错了什么。
穆萨法尔
解决方案
最终发现,通过使用“ng build”重建应用程序解决了这个问题,即使最初应用程序是在每次更改时使用 ng serve --open 重新编译的,这对我的情况来说还不够。
如果有人遇到同样的问题,希望这会有所帮助。
推荐阅读
- python - 如何测试将数据发送到接收器的浮士德代理?
- kubernetes - 我们应该为主节点和工作节点分别部署 Cluster Autoscaler 吗?
- python - Chrome 在没有无头模式的情况下无法运行(Ubuntu 18.04)
- asp.net-core - .net 核心应用程序中的 CORS 问题 - 请求的资源上不存在“Access-Control-Allow-Origin”标头
- elasticsearch - Elasticsearch 匹配查询不匹配文档
- python - IPOPT 消息和 Gekko 消息之间的区别
- c - [Visual Studio Code]:保存时运行任务
- javascript - 动态创建和获取输入
- javascript - 对象为 CSV 格式并在 JavaScript 中导出
- mysql - 如何将与 MySQL 中计算工作时间相关的查询转换为 MS Access