【问题标题】:min and max date jquery datepicker in table with ng-repeat带有ng-repeat的表中的最小和最大日期jquery datepicker
【发布时间】:2018-05-15 18:16:09
【问题描述】:

在我的页面中,我有添加、编辑和删除操作的表格。表中的一列是审计日期。日期的限制是日期值可以是从 10 分钟到填写表格前一天的任何时间。

当我在表外添加最小和最大日期限制时,它工作正常 但是我将它添加到表中的那一刻它不起作用

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

任何帮助将不胜感激

【问题讨论】:

  • 您没有在指令中使用 minminDate 选项。在演示中还使用了古老版本的 angular。希望您使用的版本比您自己的代码中的版本更新得多
  • @charlietfl 你没有在指令中使用 min 或 minDate 选项是什么意思。你能解释一下吗?

标签: javascript jquery angularjs asp.net-mvc datepicker


【解决方案1】:

您可以将您的最小/最大选项移动到您的 指令 代码中,而不是从多个位置加载 datepicker 插件。您最终会清理控制器并将选择逻辑保留在指令中:

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);
          });
        }
      });
    }
  };
});

工作sample

【讨论】:

  • 我不想这样做,因为该指令正在应用程序中的许多页面中使用。我有一个包含所有指令的实用程序 js。所以任何改变都必须在页面上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多