0

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.

sevic
  • 879
  • 11
  • 36
EliKnaffo
  • 354
  • 5
  • 17

1 Answers1

0

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.

Community
  • 1
  • 1
Jojofoulk
  • 2,127
  • 1
  • 7
  • 25
  • these two answers have problems. 1st one is not what i need it just divides them into groups. second one uses filterPredict and i already use it so i cant use it again as it creates a duplication problem. – EliKnaffo Jul 11 '19 at 06:54
  • You can just modify the `filterPredict` to keep your filtering condition and merge them with the one from the example provided. There shouldn't be duplication issues doing so. – Jojofoulk Jul 11 '19 at 07:00
  • I don't really know how to merge them i don't have much experience with doing stuff like this – EliKnaffo Jul 11 '19 at 08:27