将数组从另一个数组放入ng-grid

时间:2015-07-27 15:09:06

标签: javascript arrays json angularjs ng-grid

我有一个存储在数组(Info列)中的JSON数据,我需要将它插入到ng-grid中。所以appName,appID和appStatus都应该是最终ng-grid中的列。如何为这些字段创建字段?

JSON:

[  
   {  
      "id":75,
      "firstName":"Charlie",
      "lastName":"Latter",
      "Info":[  
         {  
            "appName":"Yale",
            "appID":21,
            "appStatus":"applied"
         },
         {  
            "appName":"NYU",
            "appID":52,
            "appStatus":"applied"
         }
      ]
   },
   {  
      "id":78,
      "firstName":"Casey",
      "lastName":"Jones",
      "Info":[  
         {  
            "appName":"Harvard",
            "appID":63,
            "appStatus":"applied"
         },
         {  
            "appName":"Princeton",
            "appID":32,
            "appStatus":"applied"
         }
      ]
   }
]

脚本:

$scope.viewCollegeApps = {
    data: 'collegeApps',
    columnDefs: [
        {field: 'id', displayName: 'ID'},
        {field: 'firstName', displayName: 'firstName'},
        {field: 'lastName', displayName: 'lastName'},
    //Fields for Info arrays//
    ],
};

1 个答案:

答案 0 :(得分:0)

除非您事先重新映射数据,否则Info数组将需要在ng-grid中显示为自己的列。

也许cellTemplates可以帮助您以合适的方式格式化信息,例如:

$scope.viewCollegeApps = {
    data: 'collegeApps',
    columnDefs: [
        {field: 'id', displayName: 'ID'},
        {field: 'firstName', displayName: 'firstName'},
        {field: 'lastName', displayName: 'lastName'},
        //Define the info column - the whole info array will appear in the Info column
        {field: 'Info', displayName: 'Info', cellTemplate: '<div ng-repeat="info in row.entity[col.field]">{{info.appName}}, {{info.appID}}</div>' }
    ],
};

或者您可以为整行执行自定义rowTemplate,这可能更合适。

https://github.com/angular-ui/ui-grid/wiki/Templating