Angular js网格验证

时间:2015-04-25 05:22:10

标签: javascript angularjs angularjs-scope ng-grid

我想添加验证,以便我的网格中年龄超过40的单元格会以红色显示?

     <html ng-app="myApp">  
     <head lang="en">
     <meta charset="utf-8">
     <title>Getting Started With ngGrid Example</title>  
     <link rel="stylesheet" type="text/css" href="ng-grid.css" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript" src="jquery-1.8.2.js"></script>
    <script type="text/javascript" src="angular.js"></script>
     <script type="text/javascript" src="ng-grid-1.3.2.js"></script>
     <script type="text/javascript">
     var app = angular.module('myApp', ['ngGrid']);
         app.controller('MyCtrl', function($scope) {
                 $scope.myData = [{name: "Moroni", age: 50},
                 {name: "Tiancum", age: 43},
                 {name: "Jacob", age: 27},
                 {name: "Nephi", age: 29},
                 {name: "Enos", age: 34}];
        $scope.gridOptions = { 
           data: 'myData',
       enableCellSelection: true
                         };
            });
          </script>
             </head>
    <body ng-controller="MyCtrl">
       <div class="gridStyle" ng-grid="gridOptions"></div>
    </body>
    </html>

我该怎么做?请帮帮我?

2 个答案:

答案 0 :(得分:2)

您可以向网格添加单元格模板

 $scope.gridOptions = { 
     data: 'myData',
     enableCellSelection: true
     columnDefs: [{cellTemplate: '<div ng-class="{red: row.getProperty(col.field) > 40}"><div class="ngCellText">{{row.getProperty(col.field)}}</div></div>'}]
};

请参阅docs的模板示例部分了解更多信息。

答案 1 :(得分:1)

你可以使用它。

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [{name: "Moroni", age: 50},
                    {name: "Tiancum", age: 43},
                    {name: "Jacob", age: 27},
                    {name: "Nephi", age: 29},
                    {name: "Enos", age: 34}];
    $scope.gridOptions = { 
        data: 'myData',
        columnDefs: [{field: 'name', displayName: 'Name'},
                     {field:'age', displayName:'Age', cellTemplate: '<div ng-class="{red: row.getProperty(col.field) > 40}"><div class="ngCellText">{{row.getProperty(col.field)}}</div></div>'}]
        };
});