AngularJS - NG Grid不允许带空格的列

时间:2014-08-23 01:35:41

标签: angularjs angular-ui ng-grid

我只是想让一个基本的Angular UI ng-grid工作。我们有来自sql server数据源的数据,其中包含空格的列,而ngGrid无法识别这些列名。有没有办法让ngGrid使用带空格的列而不修改模式?

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [{"name test": "Moroni", "age": 50},
                     {"name test": "Tiancum", "age": 43},
                     {"name test": "Jacob", "age": 27},
                     {"name test": "Nephi", "age": 29},
                     {"name test": "Enos", "age": 34}];
    $scope.gridOptions = { 
      data: 'myData',
      columnDefs: [{field:'name test', displayName:'Name'}, {field:'age', displayName:'Age'}]
    };
});

此处的Plunker代码:http://plnkr.co/edit/u6I3P8rG3PlUxsJLXqT1?p=preview

1 个答案:

答案 0 :(得分:1)

这是可能的,但您需要稍微更改一下JSON。

您可以使用括号表示法访问带空格的键:fields['name test']

你的JSON最终会看起来像这样:

$scope.myData =
    [ { fields : {"name test": "Moroni", "age": 50} },
      { fields: {"name test": "Tiancum", "age": 43} },
      { fields: {"name test": "Jacob", "age": 27} },
      { fields: {"name test": "Nephi", "age": 29} },
      { fields: {"name test": "Enos", "age": 34 } }   ] ;

您可以这样设置列定义:

columnDefs: [{field: "fields['name test']", displayName: "fields['Name']" }, {field:"fields['age']", displayName:'Age'}]

工作小提琴:

http://plnkr.co/edit/X7YAjrNXXjk1pbXftoZt?p=preview

有一个问题已经开启,但它已经关闭。您可以在此处查看更多信息:https://github.com/angular-ui/ng-grid/issues/531