简体   繁体   English

Ant Design 范围选择器禁用日期数组

[英]Ant Design range picker disable array of dates

I need to disable date before today (did it with disabledDate function).我需要在今天之前禁用日期(使用 disabledDate 函数完成)。 And I need to disable specific dates on calendar, like:我需要禁用日历上的特定日期,例如:

Today: 2018-07-30今天:2018-07-30

Dates need to disable: [2018-08-10, 2018-08-12, 2018-08-20]需要禁用的日期:[2018-08-10, 2018-08-12, 2018-08-20]

So, in this case, when I open range picker calendar, I can't select dates before today and this array of dates.所以,在这种情况下,当我打开范围选择器日历时,我无法选择今天之前的日期和这个日期数组。 What I need to do for that?我需要为此做什么?

Thanks!谢谢!

I had the same problem, when I've tried to enable just a range of values in RangePicker , using the prop disabledDate .我遇到了同样的问题,当我尝试使用道具disabledDateRangePicker启用一系列值时。

It's true that, return current && current < moment().endOf('day');确实如此, return current && current < moment().endOf('day'); works well, but not with && operator (for some reason).效果很好,但不适用于&&运算符(出于某种原因)。 Hence, for example, to enable a specific interval (eg from first January 2018 and first January 2019), you can return a Boolean for each specific date you want to disable:因此,例如,要启用特定时间间隔(例如,从 2018 年 1 月第一天到 2019 年一月第一天),您可以为要禁用的每个特定日期返回一个布尔值:

    function disabledDate(current){
    // or (if it's a class method)
    //disabledDate = (current) => { 
        let start = '2018-01-01';
        let end = '2019-01-01';
        if (current < moment(start)){
            return true;
        }
        else if (current > moment(end)){
            return true;
        }
        else {
            return false; 
        }
    }

Thus, for a specific dates I think you can adapt the same principle to achieve what you want :因此,对于特定日期,我认为您可以采用相同的原则来实现您想要的:

        function disabledDate(current){
    // or (if it's a class method)
    //disabledDate = (current) => { 
        let date1 = '2018-08-10';
        let date2 = '2018-08-12';
        let date3 = '2018-08-20';

        if (current===moment(date1)){
            return true;
        }
        else if (current===moment(date2)){
            return true;
        }
        else if (current===moment(date3)){
            return true;
        }
        else {
            return false; 
        }
    }

Finally, you put your function in the props of RangePicker :最后,你把你的函数放在 RangePicker 的 props 中:

<RangePicker disabledDate={disabledDate} />

I think RangePicker calls the function disabledDate each time we change days, months, or years, therefore, when returning a simple condition with && , it ignores the second one.我认为RangePicker每次更改天、月或年时RangePicker调用函数disabledDate ,因此,当使用&&返回一个简单条件时,它会忽略第二个条件。 But, when we verify each day with current specifically it works apparently.. (The second code for specific dates was not tested, but the first one was, and works fine!)但是,当我们用 current 验证每一天时,它显然有效..(特定日期的第二个代码没有经过测试,但第一个是,并且工作正常!)

the disabled date function compared every date rendered with your query, if you return true it will disable that date.禁用的日期函数将与您的查询呈现的每个日期进行比较,如果您返回true ,它将禁用该日期。

By this logic, if you want to have an array of enabled dates you would want to do something like the following:按照这个逻辑,如果你想要一个启用日期的数组,你会想要做如下的事情:

const dates = ['2019-05-23', '2019-05-19']
const disabledDate = current => {
    let index = dates.findIndex(date => date === moment(current).format('YYYY-MM-DD'))
    return index === -1 && true
  }

Essentially: If my date isn't within this array make it disabled.本质上:如果我的日期不在此数组中,请将其禁用。

Let's say you have something like that假设你有这样的事情

function disabledDate(current) {
  return current && current < moment().endOf('day');
}

This is the function proposed by the doc, and this allow you to disable all date before the current day (including this day).这是doc提出的功能,这允许您禁用当天(包括当天)之前的所有日期。

Now, considering you have an array containing all the dates you want to disable you could try something like that:现在,考虑到您有一个包含要禁用的所有日期的数组,您可以尝试以下操作:

const dates = ['2018-08-10', '2018-08-12', '2018-08-20'];

function disabledDate(current) { 


  return current && current < moment().endOf('day') 
  && !!dates.find(d=>current === moment(d));
}

current its all dates in the calendar that you see. current您看到的日历中的所有日期。

this.today its today. this.today 。今天就是今天。

differenceInCalendarDays() its the function to calculate how many days have between two dates. differenceInCalendarDays()它是计算两个日期之间有多少天的函数。

this.dateArrayDisabled its a array whit all my dates that I want to block. this.dateArrayDisabled它是一个包含我想要阻止的所有日期的数组。

public disabledDate = (current: Date): boolean => {
  if (differenceInCalendarDays(current, this.today) < 0) {
    return true;
  }
  for (const dateItem of this.dateArrayDisabled) {
    let days = differenceInCalendarDays(dateItem, this.today);
    if (differenceInCalendarDays(current, this.today) === days) {
      return true;
    }
  }
};

in html在 html 中

<nz-date-picker nzFormat="yyyy-MM-dd" [nzDisabledDate]="disabledDate" formControlName="date">

This works:这有效:

function disabledDate(current: any) {
for (let i = 0; i < dates.length; i++) {
  if(current.date() === moment(dates[i]).date() && current.month() === moment(dates[i]).month()){
    return false;
  }     
}
return true;  }

Where dates is an array of DateTimes.其中日期是一个日期时间数组。 Ex:前任:

['2020-05-21T22:00:00.000Z', '2020-05-22T22:00:00.000Z', '2020-05-23T22:00:00.000Z']

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

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