4

I have a mat table which is having few columns. Here for one condition I need to hide two columns. For normal table, we would use simple ngIf condition. But for this mat-table that seems to be not working. I have tried this,

   displayedColumns1: any[] = [
  'Ref No', 'E Type',
  { def: 'Approve', showMobile: true },
  { def: 'Transfer', showMobile: false },
  ];

    getDisplayedColumns(): string[] {
     const isMobile = this.entityRole === 'Admin';
     return this.displayedColumns1
      .filter(cd => !isMobile || cd.showMobile)
      .map(cd => cd.def);
    }

My HTML:

       <table mat-table [dataSource]="dataSource" class="">
        <tbody>
            <ng-container matColumnDef="Ref No">
                <th mat-header-cell *matHeaderCellDef> Ref No <br>
                </th>
                <td mat-cell *matCellDef="let element"> {{ ref }} </td>
            </ng-container>
            <ng-container matColumnDef="E Type">
                <th mat-header-cell *matHeaderCellDef> E Type <br>
                </th>
                <td mat-cell *matCellDef="let element"> {{ etype }} </td>
            </ng-container>
            <ng-container matColumnDef="Approve">
                <th mat-header-cell *matHeaderCellDef> Approve <br>
                </th>
                <td mat-cell *matCellDef="let element"> {{ Approve}} </td>
            </ng-container>
            <ng-container matColumnDef="Transfer">
                <th mat-header-cell *matHeaderCellDef> Transfer <br>
                </th>
                <td mat-cell *matCellDef="let element"> {{ Transfer }} </td>
            </ng-container>
       <tr mat-header-row *matHeaderRowDef="getDisplayedColumns"></tr>
            <tr mat-row *matRowDef="let row; columns: getDisplayedColumns;"> 
       </tr>     
        </tbody>
     </table>

But it gives ERROR TypeError: Cannot read property 'diff' of undefined error. Can anybody please suggest me how can I do that in mat-table ?

4
  • I see nothing in your code that is called diff or even dif. If you have an error about diff you should include the code that causes the error. Commented Mar 14, 2019 at 18:09
  • @Igor yes i dnt have anything called diff in my code also. Or could you please suggest me how to display few columns, based on some condition? Commented Mar 14, 2019 at 18:11
  • I couldnt use ngIF in mat table @Igor Commented Mar 14, 2019 at 18:20
  • I guess, the source of this error were the missing parentheses at <tr mat-header-row *matHeaderRowDef="getDisplayedColumns"> instead of <tr mat-header-row *matHeaderRowDef="getDisplayedColumns()"> (and in the line below) Commented Jul 19, 2021 at 6:33

2 Answers 2

3

Your issue is that your column name array is mixing a custom object with strings and your filter is not taking that into consideration.

displayedColumns1: any[] = [
    'Ref No', 
    'E Type',
    { def: 'Approve', showMobile: true },
    { def: 'Transfer', showMobile: false }
];

getDisplayedColumns(): string[] {
  const isMobile = this.entityRole === 'Admin';
  return this.displayedColumns1
    .filter(cd => !isMobile || cd['showMobile'] !== false)
    .map(cd => typeof cd === 'string' ? cd : cd.def);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your time & answer, Im not getting any error. But still that two columns are not hidiing.. could you help please?
2
displayedColumns: any[]
if (this.entityRole == 'Admin')
   { this.displayedColumns = ['name', 'dob', 'grade', 'address'] }
else
   { this.displayedColumns = ['name', 'dob', 'grade']  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.