components icon indicating copy to clipboard operation
components copied to clipboard

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

Open ghost opened this issue 8 years ago • 7 comments

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 and 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?

ghost avatar Feb 14 '18 09:02 ghost

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);
}

willshowell avatar Feb 14 '18 13:02 willshowell

Yes today it s a solution like I said. But it's really a lot of code only to do a simple "if"...

And If I want to have a button with (click)="action = true" and [matColumnDisplay]="action", I can't do it directly. I must customize the columnDefinitions and your method getDisplayedColumns once more? I don't say it doesn' work, but it's too complicated only to hide/show a column specificly.

ghost avatar Feb 14 '18 14:02 ghost

Currently when a column is in the displaylist but the 'matColumnDef' block is missing there is an error thrown, maybe if instead of the error it will just ignore it, then perhaps the column definition can be just '*ngIf'ed? Or it will cause template scope issues?

Eddygn avatar Feb 16 '18 01:02 Eddygn

The mechanism to change which columns are displayed and their order is by providing that array of column IDs. I don't think we'll support a secondary way to show/hide columns since it'll be confusing from an interface perspective. I agree that the expression approach would be nice for your use case

andrewseguin avatar Apr 23 '18 20:04 andrewseguin

https://medium.com/@ole.ersoy/hiding-angular-material-data-table-columns-f8bdd5d62abd

oleersoy avatar Sep 06 '19 02:09 oleersoy

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.

chitgoks avatar Sep 29 '19 03:09 chitgoks

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.