简体   繁体   English

如何禁用 ngb-datepicker 中除“星期一”以外的所有日期?

[英]How to disable all days except the 'Mondays' in ngb-datepicker?

I want to disable all days except the 'Mondays' or 'Fridays' etc... in ngb-datepicker?我想在 ngb-datepicker 中禁用除“星期一”或“星期五”等之外的所有日期?

Template:模板:

<input [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker" [markDisabled]="isDisabled"/>

Component:成分:

this.isDisabled = (date: NgbDate) =>(date.day) !== 1;

I realise this is almost a year old, but with that being said, this is how I resolved the same issue in my code.我意识到这已经快一年了,但话虽如此,这就是我在代码中解决相同问题的方式。

Inside the component file I specified an object containing the days to be restricted (you can specify to restrict every day as a whole, for example all tuesdays, and you can specify to restrict specific dates).在组件文件中,我指定了一个包含要限制的日期的对象(您可以指定将每一天作为一个整体进行限制,例如所有星期二,您可以指定限制特定的日期)。

dateRestrictions = {
  days: [2, 3, 4, 6, 7], // Disables day tuesday, wednesday, thursday, saturday, sunday)
  specificDates: [ // Disables the following dates in general
    { year: 2022, month: 7, day: 12 },
    { year: 2022, month: 7, day: 24 },
    { year: 2022, month: 9, day: 5 }
  ]
}

Inside the constructor, provide an instance to the NgbCalendar and define a method which will check the date provided by the date picker and determine if it should be disabled according to your rules.在构造函数中,为 NgbCalendar 提供一个实例并定义一个方法,该方法将检查日期选择器提供的日期,并根据您的规则确定是否应禁用它。 This method will need to return a boolean (represents whether the date should be restricted).此方法需要返回一个布尔值(表示是否应限制日期)。

constructor(private calendar: NgbCalendar) {
        const { specificDatesToRestrict, daysToRestrict } = this.dateRestrictions!
    this.isDisabled = (date: NgbDateStruct) => {
            return !!specificDatesToRestrict?.find(x =>{
                const restrictionDate = new NgbDate(x.year, x.month, x.day)
    
                return restrictionDate.equals(date) // If the date matches a specific date
            }) || daysToRestrict?.includes(this.calendar.getWeekday(new NgbDate(date.year,date.month,date.day))) // If the day number matches in the restriction rules
        }
}

Then, simply bind that method to the markDisabled property of your input element.然后,只需将该方法绑定到输入元素的markDisabled属性。

<input 
    class="form-control text-grey"
    placeholder="Please select a date"
    [readonly]="true"
    ngbDatepicker
    #d="ngbDatepicker"
    [markDisabled]="isDisabled"
>

This will work even if you pass a minimum and maximum date to your date picker.即使您将最小和最大日期传递给日期选择器,这也将起作用。

I think you need to inject NgbCalendar service in your component contructor我认为您需要在组件构造函数中注入 NgbCalendar 服务

import {NgbCalendar} from '@ng-bootstrap/ng-bootstrap';

constructor(private ngbCalendar: NgbCalendar) { }

and then make the function to mark as disabled days然后使 function 标记为禁用日

public isDayDisabled = (date: NgbDate) => 
   this.ngbCalendar.getWeekday(date) !== 1;

finally call the function in markDisabled最后在 markDisabled 中调用 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM