0

Angular 版本:6
库:ngbbootstrap
库参考链接:
https ://ng-bootstrap.github.io/#/components/datepicker/examples
https://ng-bootstrap.github.io/#/components/datepicker/api

使用上述库显示特定日期,我需要启用日期选择器的月份导航,但禁用日期的选择(单击)。

以下是我呈现日期选择器的代码,其中 in<ng-template>用于自定义日视图:

<ngb-datepicker #datepicker [(ngModel)]="model" (navigate)="date = $event.next" (select)="onDateSelect($event)"
            (ngModelChange)="onModelChange($event)" [dayTemplate]="templete" [disabled]="disabled" [firstDayOfWeek]="0"></ngb-datepicker>
        <ng-template #templete let-date="date">
            <span *ngIf="!showFixedDates" class="custom-day" [class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
                [class.faded]="isHovered(date) || isInside(date) || (isContains(date) && showFixedDates)"
                [class.focused]="focused && showFixedDates" [class.hidden]="isPrevious(date) || isAfter6Months(date)"
                [class.previousRangeExtremes]="isPrevious(date) && ( isFrom(date) || isTo(date))" [class.previousRange]="isPrevious(date) && isInside(date)"
                (mouseenter)="hoveredDate = date" (mouseleave)="hoveredDate = null">
                {{ date.day }}
            </span>

            <span *ngIf="showFixedDates" class="custom-day" [class.focused]="focused" 
                [class.range]="isContains(date)" (mouseenter)="hoveredDate = date" (mouseleave)="hoveredDate = null">
                {{ date.day }}
            </span>
        </ng-template>

在这方面的一些帮助会很棒!

4

1 回答 1

1

您可以使用该markDisabled功能将特定日期标记为禁用。ngb-datepicker如下设置:

<ngb-datepicker #datepicker 
    [(ngModel)]="model" 
    [markDisabled]="isDisabled"
    ...
>
</ngb-datepicker>

然后定义一个isDisabled将返回 true 的函数(这具有将所有日期标记为禁用的效果):

 isDisabled = (date: NgbDate, current: {month: number}) => true;

月份导航控件仍将处于活动状态,但无法选择日期。请参阅此 StackBlitz进行演示。

于 2018-12-11T08:42:42.997 回答