基于点击的列进行排序,在Angular Material中隐藏标题。

huangapple go评论60阅读模式
英文:

sorting based on column click in angular material with hidden headers

问题

I am using angular material version 15, here i need to sort the table but without headers, so based on click of column, sorting must take place.
HTML:

<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
  </ng-container>
  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let element" (click)="sortData('name')"> {{element.name}} </mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

Ts:

sortData(event) {
  let booleanValue: boolean = false;
  let boolean: any;
  if (boolean == true) {
    this.dataSource.sort((a, b) => a[event] < b[event] ? 1 : a[event] > b[event] ? -1 : 0);
    booleanValue = !booleanValue;
    boolean = booleanValue;
  } else {
    this.dataSource.sort((a, b) => a[event] > b[event] ? 1 : a[event] < b[event] ? -1 : 0);
    booleanValue = !booleanValue;
    boolean = booleanValue;
  }
}

Demo

英文:

I am using angular material version 15, here i need to sort the table but without headers, so based on click of column, sorting must take place.
HTML:

&lt;mat-table [dataSource]=&quot;dataSource&quot; class=&quot;mat-elevation-z8&quot;&gt;
  &lt;!-- Position Column --&gt;
  &lt;ng-container matColumnDef=&quot;position&quot;&gt;
    &lt;mat-header-cell *matHeaderCellDef&gt; No. &lt;/mat-header-cell&gt;
    &lt;mat-cell *matCellDef=&quot;let element&quot;&gt; {{element.position}} &lt;/mat-cell&gt;
  &lt;/ng-container&gt;
  &lt;!-- Name Column --&gt;
  &lt;ng-container matColumnDef=&quot;name&quot;&gt;
    &lt;mat-header-cell *matHeaderCellDef&gt; Name &lt;/mat-header-cell&gt;
    &lt;mat-cell *matCellDef=&quot;let element&quot; (click)=&quot;sortData(&#39;name&#39;)&quot;&gt; {{element.name}} &lt;/mat-cell&gt;
  &lt;/ng-container&gt;
  &lt;mat-header-row *matHeaderRowDef=&quot;displayedColumns&quot;&gt;&lt;/mat-header-row&gt;
  &lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns;&quot;&gt;&lt;/mat-row&gt;
&lt;/mat-table&gt;

Ts:

   sortData(event) {
    let booleanValue:boolean = false;
    let boolean:any;
    if (boolean == true){
      this.dataSource.sort((a, b) =&gt; a[event] &lt; b[event] ? 1 : a[event] &gt; b[event] ? -1 : 0)
      booleanValue = !booleanValue
      boolean = booleanValue
  }
  else{
      this.dataSource.sort((a, b) =&gt; a[event] &gt; b[event] ? 1 : a[event] &lt; b[event] ? -1 : 0)
      booleanValue = !booleanValue
      boolean = booleanValue
  }
  }

Demo

答案1

得分: 1

可以使用一个对象来进行排序,就像Material Angular中的使用方式一样:

sort={active: '', direction: 'asc'}

然后,您的sortData函数会使用这个对象。函数会变成这样:

sortData(event: string) {
    this.sort.direction = this.sort.active == event ? this.sort.direction == 'asc' ? 'desc' : 'asc' : 'asc';
    this.sort.active = event;

    const factor = this.sort.direction == 'asc' ? 1 : -1;
    this.dataSource.sort((a: any, b: any) => a[event] < b[event] ? factor :
        a[event] > b[event] ? -factor : 0);
    this.table.renderRows();
}

请注意,需要使用renderRows()方法来刷新表格。要获取表格,可以使用ViewChild

@ViewChild(MatTable) table: MatTable<any>;

示例链接

英文:

You can use an object sort like material angular use

sort={active:&#39;&#39;,direction:&#39;asc&#39;}

Then your function sortData use this object. Teh function will become like

sortData(event:string) {
    this.sort.direction=this.sort.active==event?this.sort.direction==&#39;asc&#39;?&#39;desc&#39;:
                                                                         &#39;asc&#39;:&#39;asc&#39;
    this.sort.active=event
      
    const factor=this.sort.direction==&#39;asc&#39;?1:-1;
     this.dataSource.sort((a:any, b:any) =&gt; a[event] &lt; b[event] ? factor :
                                            a[event] &gt; b[event] ? -factor : 0)
     this.table.renderRows()
  }

See that it's necesary use renderRows(). To get the table use ViewChild

  @ViewChild(MatTable) table: MatTable&lt;any&gt;;

stackblitz

huangapple
  • 本文由 发表于 2023年4月4日 13:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定