我需要在MAT日历中标记有任何任务的日期,但我找不到为什么这个代码不起作用.

小牛队来了

 dateClass(): any {
    return (date: Date): MatCalendarCellCssClasses => {
      const findDate = this.tasks.find((task) =>
        moment(task.date_required.toDate()).isSame(moment(date), 'day')
      );
      if (findDate !== undefined) {
        console.log(findDate)
        return 'date-with-task';
      } else {
        return;
      }
    };
  }

这些是组件导入

import {
  ChangeDetectorRef,
  Component,
  EventEmitter,
  HostListener,
  Input,
  OnChanges,
  OnInit,
  Output,
  SimpleChanges,
  ViewEncapsulation,
} from '@angular/core';

变量TASKS是来自父组件的输入

  @Input() tasks: any[];

这是我使用垫子日历的html

 <div class="calendar-container" [ngStyle]="{'display': !showingCalendar ? 'none' : ''}">
        <mat-calendar class="calendar-tasks" [dateClass]="dateClass()" [(selected)]="selectedDay"
            (selectedChange)='selectedChange($event)' (monthSelected)='monthSelected($event)' [startAt]='currentMonth'>
        </mat-calendar>
        <div class="border-bottom"></div>
    </div>

另外,我用SCSS制作了这个类,这样我就可以看看它的代码是否能正常工作

  .date-with-task{
    border-color: red;
  }

我读了所有我能找到的论坛,也看了一些关于垫子日历的YT视频,但我找不到答案.如果有人能帮助我,我将不胜感激:)

我需要找出任务标为红色的日子,但什么也没发生

推荐答案

您的代码需要进行一些更改才能按预期工作:

  1. Use an arrow function

    You're currently using a regular function. Inside it, this is the function's scope, not the class instance.
    For access to the class instance via this, you have to use an arrow function 1:

export class MyComponent {
  @Input() tasks: any[];

  dateClass = (date: Date): MatCalendarCellCssClasses => {
    console.log(this.tasks) // inside your function was `undefined`
    if (someCondition) {
      return 'some-class'
    }
    return ''
  }
}
  1. You probably don't want to style the <button />
    The class returned by the above function will be applied to button.mat-calendar-body-cell. If you style those buttons, two adjacent dates will touch each other. I suggest you style the inner cell content (.mat-calendar-body-cell-content):
.test-class .mat-calendar-body-cell-content {
  border-color: red;
}
  • 如果您决定设置按钮的样式,而不是单元格内容的样式,请注意它们当前的样式为border: none,这意味着您需要设置border-color以上的值才能使边框变为可见(border: 1px solid red;就可以了);此外,您可能希望添加一些border-radius(默认情况下它们是方框).
  1. Un-scope your (S)CSS,
    ... because you're not styling your component's DOM elements, but inner DOM elements of <mat-calendar>. To remove the CSS scoping, use ViewEncapsulation.None (or add your styles to an un-scoped stylesheet):
@Component({
  encapsulation: ViewEncapsulation.None
})

101.


1 - The function should be modified to return a string according to your business logic.
2 - The more verbose version of dateClass in the example would be:

dateClass = (date: Date): MatCalendarCellCssClasses => {
  if (
    this.tasks
      .map((t) => t.date_required)
      .find((d) => moment(d).isSame(date, 'day'))
  ) {
    return 'test-class'
  }
  return ''
}

102-或者,您可以键入dateClass作为MatCalendarCellClassFunction<Date>:

dateClass: MatCalendarCellClassFunction<Date> = (date) =>
  (this.tasks
    .map((t) => t.date_required)
    .find((d) => moment(d).isSame(date, 'day')) &&
    'test-class') ||
  '';

Angular相关问答推荐

元件不工作时的Angular 17属性 Select 器

如何在init为FALSE时以Angular 初始化主拇指擦除器-Swiper 11.0.6Angular 17 SSR

使选项卡与父零部件成一定Angular 激活

Angular :为什么我得到了一个可观察到的共鸣

HTTP拦截器在Angular 17项目中不起作用

我正在try 将一个带有模块联盟的Angular 8项目expose 到另一个Angular 项目,Angular 是15,这可能吗?如果是这样,有什么建议吗?

当可观察到的更改时,异步管道不更新视图

如何在AngularJs中剪断细绳

Angular 显示来自文字数组的SVG路径

Angular 17水化-POST请求同时触发客户端/服务器端,而GET请求仅触发服务器端?

合并Cypress和Karma的代码覆盖率报告JSON文件不会产生正确的结果

nx workspace angular monorepo 在创建新应用程序时抛出错误

可观察的等待直到另一个可观察的值

尽管模型中的变量发生了变化,但Angular 视图没有更新

如何为所有模块全局声明指令?

如何在数据表angular material中显示空消息,如果未找到数据

如何在Angular Material2中获取活动选项卡

Angular2订阅对子组件中@Input的更改

带有 Angular 的 Font Awesome 5

angular 2 http withCredentials