简体   繁体   English

带有ng-repeat的表中的最小和最大日期jquery datepicker

[英]min and max date jquery datepicker in table with ng-repeat

In my page I have table with Add, Edit and Delete operation. 在我的页面中,我具有添加,编辑和删除操作的表。 one of the column in the table is Audit Date. 表中的一列是“审核日期”。 The limitation on the date is that the date value can be anywhere from 10 minutes back to a day before the form in being filled. 日期的限制是日期值可以是填写表单之前的10分钟到前一天的任何时间。

When I am adding the min and max date limitation outside table it works fine but the minute I add it in table it does not work 当我在表外添加最小和最大日期限制时,它可以正常工作,但是当我在表中添加分钟时,它不起作用

Date Outside Table (Works Perfectly)
<input type="text" datepicker="" ng-model="auditDate" id="AddAudit" /> 

$("#AddAudit").datepicker({
    maxDate: '-1',
    minDate: '-10M'
});


Date in Table  (does not work :-( )
<table ng-repeat="i in list">
    <tr>
        <td>
            <input type="text" datepicker="" ng-model="i.startDate" ng-change="dateChanged(i)" id="EditAuditDate_{{i.id}}" />
        </td>
    </tr>    
</table>

$scope.dateChanged = function(item) {
var inpAuditDate = "#EditAuditDate_" + item.id;
    $(inpAuditDate).datepicker({
        maxDate: '-1',
        minDate: '-10M'
    });
};

I have created an example to show the issue 我创建了一个示例来显示问题

Any help will be greatly appreciated 任何帮助将不胜感激

You could move your min/max options to your directive code instead of loading the datepicker plugin from multiple places. 您可以将最小/最大选项移动到指令代码中,而不是从多个位置加载datepicker插件。 You end up cleaning your controller and keeping the selection logic in the directive: 您最终要清理控制器,并在指令中保留选择逻辑:

var app = angular.module('app', []);
app.controller('TestController', function ($scope, $window) {
    $scope.list = [{
            id: 1
        },
        {
            id: 2
        },
        {
            id: 3
        }];
});

app.directive('datepicker', function() {
  return {
    require: 'ngModel',
    link: function(scope, el, attr, ngModel) {
      $(el).datepicker({
        maxDate: '-1',
        minDate: '-10M',
        onSelect: function(dateText) {
          scope.$apply(function() {
            ngModel.$setViewValue(dateText);
          });
        }
      });
    }
  };
});

Working sample . 工作样本

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

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