简体   繁体   中英

How to groupBy rows in Mat-Table

I have mat-Table with filters and i want to group the table by one of the columns .

I want to make it a collapsible row that just shows how much you have of "that" kind and you can press it to see every single row inside that group.

I will share my HTML(that's a snippet not the whole HTML :

<mat-checkbox  class="CheckBoxClass" value="clientType" [(ngModel)]="isChecked  " disabled (change)="updateFilter('LQOCH_SHM_LOEZI_QTSR', clientType)" >{{clientType}}</mat-checkbox>
<br><br>
<mat-checkbox  class="CheckBoxClass" value="contSize" [(ngModel)]="isChecked" disabled (change)="updateFilter('AORKH_MCOLH', contSize)" >{{contSize}}</mat-checkbox>
<br><br>
<mat-checkbox  class="CheckBoxClass" value="storageType" [(ngModel)]="isChecked" disabled (change)="updateFilter('TAOR_QTSR_EBRI', storageType)" >{{storageType}}</mat-checkbox>
</div>


  <!-- Container Table -->
  <div>
    <mat-table [dataSource]="dataSource"  [hidden]="!show"  >
      <!-- Location  -->
      <ng-container matColumnDef="AITOR">
        <mat-header-cell *matHeaderCellDef> Location

        </mat-header-cell>
        <mat-cell *matCellDef="let container"> {{container.AITOR}} </mat-cell>
      </ng-container>
          <!-- Type  -->
          <ng-container matColumnDef="SOG_MCOLH">
            <mat-header-cell *matHeaderCellDef > Container Type</mat-header-cell>
            <mat-cell *matCellDef="let container"> {{container.SOG_MCOLH}}</mat-cell>

This is my component NgOnInit :

 ngOnInit() {
    this.marinService.getAllContainers().subscribe((result) => {
     //Data
      this.dataSource = new MatTableDataSource(result);
      //Paginator
      this.dataSource.paginator = this.paginator;
      this.dataSource.filterPredicate = ((data: Container, filter: string): boolean => {
        const filterValues = JSON.parse(filter);
        let conditions = true;
        for (let filterKey in filterValues) {
          if (filterKey === 'SOG_MCOLH' || filterKey === 'LQOCH_SHM_LOEZI_QTSR' || filterKey === 'AORKH_MCOLH' || filterKey === 'TAOR_QTSR_EBRI') {
              conditions = conditions && data[filterKey].trim().toLowerCase().indexOf(filterValues[filterKey]) !== -1;
          }
          else if (filterValues[filterKey].length) {
            conditions = conditions && filterValues[filterKey].includes(data[filterKey].trim().toLowerCase());
          }
        }
        return conditions;
      });
      }
      )}

Basically what I want to achieve is a row that collapse and shows how many rows it has and you can press on it to open the rows.

This doesn't come out of the box with angular Material. There are some solutions to do it yourself. Here is another SO post that gives some directions: Angular Material mat-table Row Grouping

The second StackBlitz link provided there gives a good example of a table using grouped row.

You can also have a look at other libraries that include row grouping by default, if that's something your project allows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM