在ui-grid中验证日期

时间:2015-02-12 13:27:15

标签: angularjs angular-ui-grid

我正在使用ui-grid

我的一个列有日期类型。如何通过仅接受未来日期来验证日期?

感谢。

2 个答案:

答案 0 :(得分:1)

据推测,您使用的是可编辑模块?标准日期编辑器看起来只接受有效日期。

如果你想要更多的东西,你可以为特殊的日期编辑器定义一个新的指令,并在该指令中包含验证。在创建新指令时,您需要确保提供必需的EDIT事件,请参阅教程以获取更多信息:http://ui-grid.info/docs/#/tutorial/201_editable

答案 1 :(得分:1)

您必须根据此tutorial添加自定义验证器,在您的情况下,它应该是这样的:

  1. 添加&u;-grid-validate'指向HTML中的ui-grid标记

    <div ui-grid="gridOptions" ui-grid-edit ui-grid-cellNav ui-grid-validate        
    class="grid"></div>
    
  2. Inject&#39; uiGridValidateService&#39;控制器中的服务.js

    app.controller('MainCtrl', ['$scope', 'uiGridValidateService', function ($scope, uiGridValidateService)
    
  3. 声明日期的列定义,如下所示:

    $scope.gridOptions.columnDefs = [
       //other columns
       { name: 'myDate', displayName: 'my date',  
       validators: {
             required: true, compareDateNow: ''
       }, 
       cellTemplate: 'ui-grid/cellTitleValidator' }
    ];
    
  4. 实施您的验证方法,该方法在单元格值和当前日期之间徘徊:

    uiGridValidateService.setValidator('compareDateNow', 
      function (argument){
        return function (oldValue, newValue, rowEntity, colDef) {
            if (!newValue) {
                return true;
            } else {
                return (newValue >= Date.now());
            }
        };
      },
      function (argument) {
        return 'error message';
      }
    );