首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular mat-table尝试实现mat-sort

angular mat-table尝试实现mat-sort
EN

Stack Overflow用户
提问于 2019-11-19 18:37:31
回答 1查看 245关注 0票数 1

我正尝试在mat表上实现mat-sort,但我在尝试初始化mat排序功能时遇到了一些问题。排序属性未定义:

垫子表:

代码语言:javascript
复制
<mat-table matSort #sort1="matSort" matSortActive="id" matSortDirection="asc" matSortDisableClear
                                               #table
                                               [dataSource]="dataSource">
                                        <ng-container matColumnDef="statusName">
                                            <mat-header-cell *matHeaderCellDef mat-sort-header="customer.statusName">Status Name</mat-header-cell>
                                            <mat-cell *matCellDef="let customer">
                                                {{customer.statusName}}
                                            </mat-cell>
                                        </ng-container>
                                    </mat-table>

组件代码:

代码语言:javascript
复制
@ViewChild('sort1', { static: true }) sort: MatSort;
ngOnInit(): void {

    const sortSubscription = this.sort.sortChange.subscribe(() => (this.paginator.pageIndex = 0));
    this.subscriptions.push(sortSubscription);

    this.dataSource = new InvoiceStatusDataSource(this.invoiceStatusService);

    this.loadItems(true);
}

在loadItems中,我有这样的功能:

代码语言:javascript
复制
 loadItems(firstLoad: boolean = false) {
        const queryParams = new QueryParamsModel(
            {},
            this.sort.direction,
            this.sort.active,
            this.paginator.pageIndex,
            this.paginator.pageSize
        );
        this.dataSource.loadItems(queryParams);
        this.selection.clear();
    }

正如您在顶部的屏幕截图中所看到的,排序属性是未定义的。我是否需要编写一些额外的代码来初始化该属性?

EN

回答 1

Stack Overflow用户

发布于 2019-11-19 19:08:50

您的MatTable是否封装在任何ngIf中?如果为true,你必须在ngAfterViewInit中设置你的排序,因为你的表没有被呈现,你的'MatSort‘不存在,或者你可以添加一个设置器,当你的MatSort存在的时候设置它。

代码语言:javascript
复制
@ViewChild('sort1', { static: false})set matSort(value: MatSort){
  if(this.dataSource && value) {
    this.dataSource.sort = value;
  }
}
代码语言:javascript
复制
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58932037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档