如何在禁用日期选择的情况下在ngbdatepicker中允许月份导航?

时间:2018-12-07 14:33:35

标签: angular6 ng-bootstrap ngb-datepicker

角度版本:6
库:ngbbootstrap
图书馆参考链接:
https://ng-bootstrap.github.io/#/components/datepicker/examples
https://ng-bootstrap.github.io/#/components/datepicker/api

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

以下是我渲染日期选择器的代码,其中<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>

对此会有一些帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用markDisabled函数将特定日期标记为已禁用。在您的ngb-datepicker上进行以下设置:

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

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

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

月份导航控件仍将处于活动状态,但没有日期可供选择。请参见this StackBlitz进行演示。