本文介绍了如何使用角度材料实现范围内嵌日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用材料日历作为范围内嵌日历来显示和插入日期范围.当使用 mat-date-range-picker 时,这只是工作(但不是内联).使用 mat-calendar 时,它适用于内联,但不适用于 ranged.但是,如果我将 selectedRangeValue 作为 DateRange 而不是 Date 传递,则范围将正确显示.

I want to use material calendar as a ranged inline calendar to display and insert date ranges.When using mat-date-range-picker, this is just working (but not inline).When using mat-calendar, it is working for inline, but not for ranged.But if I pass selectedRangeValue as DateRange instead of Date, the Range is displayed properly.

唯一还缺少的是输入...

The only thing still missing is input...

这是我现在使用的代码(缩写):

This is the code I use now (abbreviated):

<mat-calendar (selectedChange)="selectedRangeChange($event)"
              [selected]="selectedRangeValue"
>
</mat-calendar>
selectedRangeValue: DateRange<Date> =  new DateRange<Date>(this.selectedValue.begin, this.selectedValue.end);

我必须这样做,因为土星日期选择器只支持到 Angular 9,从 Angular 10 开始,Material Datepicker 支持日期范围.但是这个内嵌日期范围日历"我就是不能工作...

I have to do this because Saturn Date picker is only supported until Angular 9, and from Angular 10 onwards Material Datepicker supports date ranges.But this "inline date range calendar" I just cannot make work...

我找到了 https://github.com/angular/components/issues/20697 描述了相同的问题并找到了解决方案,但没有写下来,因此无济于事.

I found https://github.com/angular/components/issues/20697 where the same problem is described and a solution was found, but not written down, so that does not help.

我也尝试过了解angular material datepicker的源码,可惜还是没搞懂.如果您有任何帮助或提示,我将不胜感激.

I also tried to understand the source code of angular material datepicker, but unfortunately I still don't get it. I would appreciate any help or hints.

推荐答案

我找到了解决这个问题的方法.虽然我不确定这是否是最好的方法,但我想提供解决方案.

I found a way to solve this. While I am not sure if this is the best way, I want to provide the solution.

我必须为内联范围日历创建一个新的角度组件:

I had to create a new angular component for the inline ranged calendar:

HTML 架构

<mat-calendar
  #calendar
  [selected]="selectedRangeValue"
  (selectedChange)="selectedChange($event)"
>
</mat-calendar>

打字稿

import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {
  DateRange, DefaultMatCalendarRangeStrategy,
  MAT_DATE_RANGE_SELECTION_STRATEGY, MatCalendar,
} from '@angular/material/datepicker';
import {isNullOrUndefined} from '../../util/is-null-or-undefined';
import moment from 'moment';

@Component({
  // tslint:disable-next-line:component-selector
  selector: 'inline-range-calendar',
  templateUrl: './inline-range-calendar.component.html',
  styleUrls: ['./inline-range-calendar.component.sass'],
  providers: [{
    provide: MAT_DATE_RANGE_SELECTION_STRATEGY,
    useClass: DefaultMatCalendarRangeStrategy
  }]
})
export class InlineRangeCalendarComponent implements OnInit {

  @ViewChild(MatCalendar) calendar: MatCalendar<Date>;

  @Input() selectedRangeValue: DateRange<Date>;
  @Output() selectedRangeValueChange = new EventEmitter<DateRange<Date>>();

  ngOnInit(): void {
 }

selectedChange($event) {
const m = moment($event);

if (!isNullOrUndefined(this.selectedRangeValue.end)) {
  const start = this.selectedRangeValue.start;
  // @ts-ignore the parser thinks that this is a date, but it is a moment.js object, so this will work
  start.set(m.toObject());
  this.selectedRangeValue = new DateRange<Date>(start, undefined);
  this.selectedRangeValueChange.emit(this.selectedRangeValue);
} else {
  const end = (!isNullOrUndefined(this.selectedRangeValue.end)) ? this.selectedRangeValue.end : moment(m);
  // @ts-ignore the parser thinks that this is a date, but it is a moment.js object, so this will work
  end.set(m.toObject());
  // @ts-ignore the parser thinks that this is a date, but it is a moment.js object, so this will work
  this.selectedRangeValue = new DateRange<Date>(this.selectedRangeValue.start, end);
  if (this.selectedRangeValue.end < this.selectedRangeValue.start)  {
    this.selectedRangeValue = new DateRange<Date>(this.selectedRangeValue.end, this.selectedRangeValue.start);
  }
  this.selectedRangeValueChange.emit(this.selectedRangeValue);
}
}

}

希望这可以帮助某人.

这篇关于如何使用角度材料实现范围内嵌日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:34