在Angular Material MatSortable

时间:2018-09-20 21:53:43

标签: angular angular-material2

在MatSortable的排序方向上有三种状态。有没有办法禁用第三种状态?它有'asc' | 'desc' | ''个ID,就像只有'asc' | 'desc'个可用。我目前正在过滤排序方向,但是我想知道,即使标题确实显示了具有当前排序方向的箭头,从用户的角度来看,这是否足够直观以至看起来也不像是一个错误(见下图)。

Oninit生命周期挂钩我正在设置默认排序方式,以为将disableClear设置为true可以解决此问题,但不能进行。任何帮助表示赞赏!

defaultSort: MatSortable = {
  id: 'displayName',
  start: 'asc',
  /**
   * Whether to disable the user from clearing the sort by finishing the sort direction cycle.
   * May be overriden by the MatSortable's disable clear input.
   */
  disableClear: True
};

ngOnInit() {
  this.sort.sort(this.defaultSort);

  this.sort.sortChange.pipe(
    filter(sort => !!sort.direction),
    switchMap(sort => {
      // makes API request only with an actual direction.
    })
  );
}

无排序方向: No sort direction

带有排序说明: asc enter image description here

2 个答案:

答案 0 :(得分:5)

添加提到的Flignats行对我不起作用,因此我必须在模板中添加disableClear,例如:

<th mat-sort-header="firstName" disableClear>First name</th>

我希望这会对某人有所帮助。

答案 1 :(得分:2)

我不清楚您的代码,但这类似于以下内容:

您的组件上有这个

@ViewChild(MatSort) sort: MatSort;

  ngOnInit() {
    ....
    this.dataSource.sort = this.sort;
  }

在定义排序后添加此行

this.sort.disableClear = true;

Example Stackblitz