Angular 5 Mat-grid list responsive

后端 未结 6 1687
灰色年华
灰色年华 2021-01-30 03:08

i created grid list with column 6 and i want to be grid title take 100% width on small screen devices. Now it creates 6 column on small screens as well

Expected: One gri

6条回答
  •  情歌与酒
    2021-01-30 03:41

    You may use the following directive if you need a reusable solution:

     
    import { Directive, Input, OnInit } from '@angular/core';
    import { MatGridList } from '@angular/material';
    import { ObservableMedia, MediaChange } from '@angular/flex-layout';
    
    export interface IResponsiveColumnsMap {
      xs?: number;
      sm?: number;
      md?: number;
      lg?: number;
      xl?: number;
    }
    
    // Usage: 
    @Directive({
      selector: '[responsiveCols]'
    })
    export class ResponsiveColsDirective implements OnInit {
      private countBySize: IResponsiveColumnsMap = {xs: 2, sm: 2, md: 4, lg: 6, xl: 8};
    
      public get cols(): IResponsiveColumnsMap {
        return this.countBySize;
      }
    
      @Input('responsiveCols')
      public set cols(map: IResponsiveColumnsMap) {
        if (map && ('object' === (typeof map))) {
          this.countBySize = map;
        }
      }
    
      public constructor(
        private grid: MatGridList,
        private media: ObservableMedia
      ) {
        this.initializeColsCount();
      }
    
      public ngOnInit(): void {
        this.initializeColsCount();
    
        this.media.asObservable()
          .subscribe((changes: MediaChange) => 
            this.grid.cols = this.countBySize[changes.mqAlias]
          );
      }
    
      private initializeColsCount(): void {
        Object.keys(this.countBySize).some( 
          (mqAlias: string): boolean => {
            const isActive = this.media.isActive(mqAlias);
    
            if (isActive) {
              this.grid.cols = this.countBySize[mqAlias];
            }
    
            return isActive;
        });
      }
    }

提交回复
热议问题