简体   繁体   English

如果日期小于当前日期,则禁用ui bootstrap datepicker-AngularJS ngRepeat

[英]Disable ui bootstrap datepicker if the date is less than the current date - Angularjs ngRepeat

How can I disable each individual datepicker if the date model is less than the current date or if the date has "expired". 如果日期模型小于当前日期或日期已“过期”,如何禁用每个单独的日期选择器。

I am developing a UI with datepickers for start and end dates. 我正在开发一个带有Datepickers的UI,用于开始和结束日期。 Initially, the UI can have as many datepickers as needed depending on the data returned from a backend. 最初,UI可以根据需要具有多个日期选择器,具体取决于从后端返回的数据。 Users can also add more date pickers. 用户还可以添加更多日期选择器。

Here is a sample data I am using to build datepickers with ngRepeat . 这是我正在使用ngRepeat构建ngRepeat的示例数据。

        {
            "id": 1234,
            "seasons": [{
                "endDate": "2016-01-03",
                "startDate": "2015-09-10",
                "description": "2015"
            }, {
                "endDate": "2017-01-03",
                "startDate": "2016-09-10",
                "description": "2016"
            }]
        }

I am creating a UI where users can change dates via datepickers only if the start and end date has not expired. 我正在创建一个UI,用户只有在开始日期和结束日期尚未到期的情况下,才能通过日期选择器更改日期。 In cases where the date has expired the datepicker needs to be disable. 如果日期已过期,则需要禁用日期选择器。

Here is my UI for reference. 这是我的UI供参考。 在此处输入图片说明

My current approach is to iterate through seasons array and check if the startDate is less than today. 我当前的方法是遍历seasons数组,并检查startDate是否小于今天。

        ss.datePickerStartDateEnabled = false;

        angular.forEach(ss.seasonSet.seasons, function(season, key) {
            if (season.startDate < today) {
                console.log('Less than today');
                ss.datePickerStartDateEnabled = true;
            }
        });

So far it works but it disables startDate that is not less than today. 到目前为止,它仍然有效,但是它禁用了不少于今天的startDate。

Here's my html 这是我的HTML

<div class="row" ng-repeat="s in ss.seasonSet.seasons track by $index">
   <div ng-controller="DatePickerCtrl as datePicker">
      <div class="col-md-3">                      <!-- StartDate datepicker-->
         <div class="form-group has-feedback">
            <label for="username">Start Date</label>
            <input 
               type="text" 
               class="form-control" 
               id="startDate{{$index}}" <!-- id=startDate1 -->
               uib-datepicker-popup="{{}}" 
               ng-model="s.startDate" 
               is-open="datePicker.isOpen.startDate" 
               datepicker-options="datePicker.dateOptions" 
               ng-required="true"
               ng-disabled="ss.datePickerStartDateEnabled" 
               close-text="Close"
               ng-click="datePicker.open($event, 'startDate')"/>
            <span class="form-control-feedback glyphicon glyphicon-calendar"></span>
         </div>
         <div class="form-group has-feedback">
            <label for="username">End Date</label>
            <input 
               type="text" 
               class="form-control"
               id="endDate+{{$index}}" 
               uib-datepicker-popup="{{}}" 
               ng-model="s.endDate" 
               is-open="datePicker.isOpen.endDate" 
               datepicker-options="datePicker.dateOptions" 
               ng-required="true"
               close-text="Close"
               ng-click="datePicker.open($event, 'endDate')"/>
            <span class="form-control-feedback glyphicon glyphicon-calendar"></span>
         </div>
      </div>
   </div>
</div>

How could I use id="endDate+{{$index}}" in the datepicker input along with the ng-disabled="ss.datePickerStartDateEnabled" and ss.datePickerEndtDateEnabled in my controller to disable a single date picker based on the condition from above. 如何在日期选择器输入中使用id="endDate+{{$index}}"以及控制器中的ng-disabled="ss.datePickerStartDateEnabled"ss.datePickerEndtDateEnabled来根据上述条件禁用单个日期选择器。

There are other validations that I need to do eg no overlapping dates and start date must be after the previous end date. 我还需要进行其他验证,例如,没有重叠的日期,并且开始日期必须在上一个结束日期之后。 I am trying to solve the easy case first. 我正在尝试先解决简单的情况。

Thanks in advance. 提前致谢。 Here is the the plunker code and here is the UI , tho the datetime picker is not working. 这是插入代码 ,这是UI ,日期时间选择器不起作用。 See it full screen for better UI. 全屏查看它以获得更好的UI。

Using ng-disabled on the input is the right approach. input上使用ng-disabled是正确的方法。

Your comment, 你的评论,

So far it works but it disables startDate that is not less than today. 到目前为止,它仍然有效,但是它禁用了不少于今天的startDate。

Makes me think that your date comparison may be flawed, for example, if they are comparing different date formats. 让我认为您的日期比较可能有缺陷,例如,如果他们比较不同的日期格式。 After the date comparison, disabling the input via ng-disabled should be all there is to this. 在日期比较之后,通过ng-disabled禁用input应该就可以了。

You also have some semantic confusion in ng-disabled="ss.datePickerStartDateEnabled" Where it essentially says "disable if enabled". ng-disabled="ss.datePickerStartDateEnabled"中,您还会有一些语义上的困惑, ng-disabled="ss.datePickerStartDateEnabled"它说的是“如果启用则禁用”。

I was able to solve this problem using the following pattern. 我可以使用以下模式解决此问题。

I delegated the UI logic to disable datepicker(s) if the date has expired to my DatePickerCtrl . 如果日期过期到我的DatePickerCtrl我委派了UI逻辑以禁用datepicker。

angular.module('someApp')
.controller('DatePickerCtrl', ['$filter', function($filter) {

    var datePicker = this;

    datePicker.dateOptions = {
        formatYear: 'yy',
        startingDay: 1,
    };

    var today = $filter('date')(new Date(), "yyyy-MM-dd");

    datePicker.startDateDisable = startDateDisable;
    datePicker.endDateDisable = endDateDisable;
    datePicker.open = open;

    //Useful to manage more than one datepicker in the same view
    //E.g. datepickers created in a ngRepeat
    datePicker.isOpen = {};

    function open($event, which) {
        $event.preventDefault();
        $event.stopPropagation();
        datePicker.isOpen[which] = true;
    }

    function startDateDisable(startDate) {
        return startDate <= today ? true : false;
    }

    function endDateDisable(endDate) {
        return endDate <= today ? true : false;
    }

    //Date format could come from user's profile preferences
    datePicker.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    datePicker.format = datePicker.formats[2];
}])

The startDateDisable(startDate) and endDateDisable(endDate) functions are the only relevant ones. startDateDisable(startDate)endDateDisable(endDate)函数是唯一相关的函数。

Next, I used ng-disabled directive on the datepicker input as such: 接下来,我在datepicker输入上使用了ng-disabled指令,如下所示:

<div class="form-group has-feedback">
   <label for="username">Start Date</label>
   <input 
      type="text" 
      class="form-control" 
      id="startDate{{$index}}"
      uib-datepicker-popup="{{format}}" 
      ng-model="s.startDate" 
      is-open="datePicker.isOpen.startDate" 
      datepicker-options="datePicker.dateOptions" 
      ng-required="true"
      ng-disabled="datePicker.startDateDisable(s.startDate)"
      close-text="Close"
      ng-click="datePicker.open($event, 'startDate')"
      date-validation/>
   <span class="form-control-feedback glyphicon glyphicon-calendar"></span>
</div>

The relevant code is ng-disabled="datePicker.startDateDisable(s.startDate)" where s.startDate in my input model. 相关代码为ng-disabled="datePicker.startDateDisable(s.startDate)" ,其中s.startDate在我的输入模型中。 The DatePickerCtrl is responsible for managing the UI state. DatePickerCtrl负责管理UI状态。 The data comes from another controller. 数据来自另一个控制器。

The same logic applies for endDate. 相同的逻辑适用于endDate。

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

相关问题 jQuery UI Datepicker - 禁用当前日期但不是高亮显示 - jQuery UI Datepicker - Disable current date but NOT the highlight Twitter Bootstrap Datepicker-开始日期小于结束日期 - Twitter bootstrap datepicker - start date less than end date 在JQuery UI datepicker中禁用当前日期之前的日期 - Disable date before current date in JQuery UI datepicker 如何使用 JavaScript 或 Bootstrap 在日期选择器中禁用当前日期和未来日期 - How to disable the current date and future dates in datepicker using JavaScript or Bootstrap 如何禁用/限制用户在引导日期选择器中选择未来和当前日期? - How to disable/restrict user to select future and current date in bootstrap datepicker? 禁用日期选择器中距当前日期超过2天的日期 - Disable dates further than 2 days from current date in Datepicker Bootstrap Datepicker禁用特定日期 - Bootstrap datepicker disable specific date 如何使用angularjs检查所选日期和所选时间小于当前日期或大于当前日期和时间? - How to check selected date and selected time is less than current or greater than current date and time using angularjs? 通过设置当前日期来禁用日期选择-DatePicker - Disable date selection by setting the current date - DatePicker 防止在Bootstrap Datepicker中选择a to date - Preventing choosing a to date less then from in Bootstrap Datepicker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM