【问题标题】:customize the mat-calendar, remove month label自定义 mat-calendar,删除月份标签
【发布时间】:2020-03-08 14:32:57
【问题描述】:

我正在尝试从角材料垫日历中删除月份标签。 这是stackblitz链接

https://stackblitz.com/edit/am-all-imports-mviy6e?file=styles.scss

  <mat-calendar [dateClass]="dateClass()" [selected]="selectedDate" (selectedChange)="onSelect($event)"></mat-calendar>


onSelect(event){
    console.log(event);
    this.selectedDate = event;
  }

  dateClass() {
    return (date: Date): MatCalendarCellCssClasses => {
      const highlightDate = this.datesToHighlight
        .map(strDate => new Date(strDate))
        .some(d => d.getDate() === date.getDate() && d.getMonth() === date.getMonth() && d.getFullYear() === date.getFullYear());

      return highlightDate ? 'special-date' : '';
    };

}

对于css:-

.mat-calendar-body-label{
   display: none;
  }

如果我使用上面的 css 代码,它会从左到右移动日历日期并使日历变形。

如果我使用这个:-

.mat-calendar-body-label{
  opacity: 0;
  }

如果没有日期,它会留下空行

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    除了:

    .mat-calendar-body-label{
         opacity: 0;
    }
    

    添加以下内容以消除当月的第一天从第一列开始时的额外空间:

    .mat-calendar-body-label[colspan="7"] {
        display: none;
    }
    

    【讨论】:

    • 我有边框,所以它删除了边框
    • 所以你想要曾经是一个月的空盒子?
    • 好的,我想我现在明白了。继续使用不透明度:0;通过添加 [colspan="7"] 规则来隐藏月份并保持外边界。这应该够了吧。我将编辑我的答案。
    【解决方案2】:

    由于封装需要使用阴影穿透来获得日历的样式,所以使用 ng-deep 选择器。

    ::ng-deep .mat-calendar-body-label{
       display: none;
      }
    

    【讨论】:

    • 这仍然会导致日期转移。此外,如果此规则设置在父级别,则不需要 ng-deep。
    • 我做了封装:ViewEncapsulation.None
    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多