简体   繁体   English

如何在角材料datepicker中禁用特定日期

[英]How to disable specific dates in angular material datepicker

How can I disable specific dates based on the current date, let's say that today is 8/1/16, I want to disable 4 days (4 it's an example it could be any number) after the current date weekends days doesn't count, 如何根据当前日期禁用特定日期,假设今天是8/1/16,我想在当前日期之后禁用4天(4个例子,它可以是任何数字),周末天数不计算,

Example if today is 8/1/16 I want to disable 8/2/16 , 8/3/16, 8/4/16 and 8/5/16, and the dates that would be available would be the ones that are after 8/8/16. 示例如果今天是8/1/16我想要禁用8/2 / 16,8 / 3 / 16,8 / 4/16和8/5/16,并且可用的日期将是那些16/8/16之后。

At the moment I only know how to disable all the weekend days in the calendar with this filter 目前我只知道如何使用此过滤器禁用日历中的所有周末日

$scope.disableSelectedDays = function () {

                //I create a New moment since I always going to need the current date
                var momentJS = new moment();

                if (momentJS <= moment().add(4, 'days')) {
                    return false;
                } else {
                    //Just disable the weekends
                    var day = date.getDay();
                    return day === 1 || day === 2 || day === 3 || day === 4 || day === 5;
                }
            }

this is my html 这是我的HTML

<div class="form-group">
                                <label>Delivery date:</label>
                                <md-datepicker ng-model="myDate" md-placeholder="Enter date"
                                               md-date-filter="disableSelectedDays">
                                </md-datepicker>
                            </div>

this are the versions that I'm using angular-maeria: v1.1.0-rc.5 datepicker: datepicker from angular material v1.1.0-rc4-master-88f2e3f 这是我正在使用angular-maeria的版本:v1.1.0-rc.5 datepicker:来自angular material v1.1.0-rc4-master-88f2e3f的datepicker

EDIT 1 编辑1

I edited my code with your answer but now all the days in the datepicker are disabled I can't select anything, what I'm doing wrong? 我用你的答案编辑了我的代码,但现在日期选择器中的所有日子都被禁用了我无法选择任何东西,我做错了什么?

I added this libaries moment libraries 我添加了这个库的时刻库

moment.js version : 2.14.1 angular-moment https://github.com/urish/angular-moment moment.js版本:2.14.1 angular-moment https://github.com/urish/angular-moment

For this scenario, you can try using moment.js 对于此场景,您可以尝试使用moment.js

To disable any number of days after the selected date, you can do this (Hopefully should work, have not tested, there might be typo's) 要在所选日期之后禁用任意天数,您可以执行此操作(希望能够正常工作,未经测试,可能存在拼写错误)

//date is the date you wanted to check // date是您要检查的日期

//NumberOfdays 4 as per your example //根据你的例子,NumberOfdays 4

$scope.disableSelectedDays = function (date ) {
    if(date <= moment().add(4, 'days')) {
        return false;
    }
    else {
    //Just disable the weekends
            var day = date.getDay();
            return day === 1 || day === 2 || day ===3 || day === 4 || day === 5;
         }
  }

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

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