question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Mat-Table] Feature Request : Add possibility to "hide" some columns directly in template

See original GitHub issue

Bug, feature request, or proposal:

Possibility to hide some columns directly in template

What is the expected behavior?

Actually, I have a datable like this:

<mat-table #table [dataSource]="dataSource">
            <ng-container matColumnDef="col1">
                      <mat-header-cell *matHeaderCellDef>h1</mat-header-cell>
                      <mat-cell *matCellDef="let element">c1</mat-cell>
            </ng-container>
            <ng-container matColumnDef="col2">
                      <mat-header-cell *matHeaderCellDef>h2</mat-header-cell>
                      <mat-cell *matCellDef="let element">c2</mat-cell>
            </ng-container>
</mat-table>

I would like to have something like this (by exemple):

            <ng-container matColumnDef="col1" [matColumnDisplay]="expr">
                      <mat-header-cell *matHeaderCellDef>h1</mat-header-cell>
                      <mat-cell *matCellDef="let element">c1</mat-cell>
            </ng-container>

matColumnDisplay is a boolean expression to know if this column must be displayed or not (===> removed from the DOM)

or why not: <ng-container matColumnDef="col1" *ngIf="expr"> ==> but it causes an error

By exemple: [matColumnDisplay]="currentDisplay === 'mobile'" or [matColumnDisplay]="isColumnHidden === myVar"

What is the current behavior?

The only possible today is: add css class to <mat-header-cell> and <mat-cell> By exemple: <mat-header-cell class="d-none d-md-block"> ==> Work but, the element is not remove from the DOM and lots of css class to add.

Play with matHeaderRowDef and matRowDef and have several variables: const menuMobile = ['col1']; const menuDesktop = ['col1', 'col2', ...]; const menuInTheCaseOf = ['col1', 'col3', ...]; ==> Work but, add a lot of codes to manage several cases

What are the steps to reproduce?

What is the use-case or motivation for changing an existing behavior?

To have the simplest and shortest code to manage responsive case or fonctional case in columns display

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

5.2.x

Is there anything else we should know?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
willshowellcommented, Feb 14, 2018

I think the expected way to handle this is to update the columns passed to your row template:

<mat-row *matRowDef="let row; columns: getDisplayedColumns();"></mat-row>
const columnDefinitions = [
  { def: 'col1', showMobile: true },
  { def: 'col2', showMobile: false },
];
 
getDisplayedColumns(): string[] {
  const isMobile = this.currentDisplay === 'mobile';
  return this.columnDefinitions
    .filter(cd => !isMobile || cd.showMobile)
    .map(cd => cd.def);
}
3reactions
chitgokscommented, Sep 29, 2019

this should be a very nice feature. sometimes we need to hide columns based on the screen width. such columns will not fit in mobile devices if there are too many of them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular2 material table hide column - Stack Overflow
I need to hide a column in the table by some boolean condition. Is it possible without changing columns map in my component?...
Read more >
Hiding Angular Material Data Table Columns | by Ole Ersoy
We have a material data table with the colums id and Decription . We want to have two mat-checkbox fields, one for id...
Read more >
Table | Angular Material
Begin by adding the <table mat-table> component to your template and passing in data. ... Here's a simple column definition with the name...
Read more >
Use Ngif To Hide A Row In Mattable - ADocLib
Possibility to hide some columns directly in template Ask questions[MatTable] Feature Request : Add possibility to hide some columns.
Read more >
Angular Material Data Table: A Complete Example
MatTableModule: this is the core data table module, which includes the mat-table component and many related components and directives ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found