【问题标题】:How to sort angular material design table when rendering/creating渲染/创建时如何对角度材料设计表进行排序
【发布时间】:2021-07-03 19:49:29
【问题描述】:

我成功地为我的角度材料设计表应用了排序机制,但我缺少的是当我加载页面时表会自行排序。当我单击要排序的列的列标题但在启动时不起作用时,它可以工作。我能找到的唯一解决方案是 (matSortChange) 但我不想定义排序函数,因为显然 angular 知道排序函数,但只是在创建表时不应用它。我可能面临的一个问题是我将 dataSource 创建为空,因为我在 ngOnInit() 函数中添加了数据。

我也尝试了 matSortActive="name" matSortDirection="desc" 并且箭头在那里但它没有排序,可能是因为在更改数字之前我的开头只有 0。但我不知道我怎么能推迟这个。

问候

【问题讨论】:

标签: angular angular-material


【解决方案1】:

只是对数据源进行排序:

ngAfterViewInit() {
    this.sort.active="name";
    this.sort.direction="asc"
    this.dataSource.sort = this.sort;
}

stackblitz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2020-04-30
    • 2021-03-04
    • 1970-01-01
    • 2019-12-27
    • 2015-04-16
    • 2015-10-16
    相关资源
    最近更新 更多