UI 网格 Angular,网格呈现但不显示数据

UI Grid Angular, grid renders but doesn't display data

我在这里错过了什么?网格呈现没有错误,空白行...我检查过 JSON 到目前为止一切正常 $scope.gridOptions.data = response['data']; 它似乎呈现空数组并且永远不会获取实际数据.. .

  app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
        $scope.myData = [];
        $scope.loading = true;

        $scope.gridOptions = {
            enableSorting: true,
            columnDefs: [
              { name: 'Id', field: 'PK_Inspection' },
              { name: 'Employee Id', field: 'EmployeeId' },
              { name: 'Employee Name', field: 'EmployeeName' },
              { name: 'Equipment Id', field: 'EquipmentId' },
              { name: 'Equipment Name', field: 'EquipmentName' },
              { name: 'Sequence', field: 'SequenceName' },
              { name: 'Details', field: 'SequenceDetails' },
              { name: 'Type', field: 'SequenceTypeName' },
              { name: 'Shift', field: 'ShiftName' },
              { name: 'Status', field: 'StatusName' }
            ],
            data:[]
        };

        $http.get('/Home/GetAllData')
            .then(function (response) {
                $scope.gridOptions.data = response['data'];
            })
            .catch(function (err) {
                $scope.loading = false;
                console.log("Error Receiving Data.");
            })
            .finally(function () {
                $scope.loading = false;
                console.log($scope.gridOptions.data);

            });

    }]);

正在传递给 $scope.gridOptions.data 的数据:

[
    {
        "PK_Inspection": 1,
        "EmployeeId": "4433112",
        "EmployeeName": "",
        "EquipmentId": "1122113",
        "EquipmentName": "",
        "SequenceName": "UNIT 1",
        "SequenceDetails": "Bent, Dented, Broken, Torn or Deformed Parts.",
        "SequenceTypeName": "Visual Inspection",
        "ShiftName": "Day",
        "StatusName": "OK"
    },
    {
        "PK_Inspection": 2,
        "EmployeeId": "4433112",
        "EmployeeName": "",
        "EquipmentId": "1122113",
        "EquipmentName": "",
        "SequenceName": "UNIT 2",
        "SequenceDetails": "Charge, Water Levels, Vent Caps in place, Power Disconnect works.",
        "SequenceTypeName": "Visual Inspection",
        "ShiftName": "Day",
        "StatusName": "OK"
    }
]

这是HTML:

<div ng-controller="MainCtrl">
    <i class="fa fa-spinner fa-spin text-center" ng-show="loading"></i>
    <div id="mainGrid" ui-grid="gridOptions" ui-grid-edit class="myGrid"></div>
</div>

截图

您从响应中访问了错误的 JSON 数据(取自您的响应示例,数组不在 'data' 名称中)。您的响应包含带有数据的无名数组,在您的代码中:

$scope.gridOptions.data = response['data'];

应该是:

$scope.gridOptions.data = response;

我弄明白了,看来我的问题是两件事的混合体。

  1. 传入的 JSON 是一个字符串,我不是 100% 确定我是否需要使用 JSON.parse 转换为对象然后将其传递给 $scope.gridOptions.data 但是这可能是我在上面的原始问题中发布的代码的问题。
  2. 经过更多研究,我在官方 Angular UI 网格文档中找到了一个很好的深入示例。按照这种技术,我能够正确呈现数据。

    var rowCount = 0;
    var i = 0;
    $scope.refreshData = function () {
        $scope.loading = true;
        $scope.myData = [];
    
        $http.get('/Home/GetAllData')
            .success(function (response) {
                var jsonObj = JSON.parse(response);
                rowCount = jsonObj.length;
    
                jsonObj.forEach(function (row) {
                    row.id = i; i++;
                    $scope.myData.push(row);
                });
                $scope.loading = false;
    
            })
            .error(function() {
                $scope.loading = false;
                console.log("Error retrieving data.");
            });
     };
    

在示例中,它使用了 gridOptions.data 中的一个字符串值,称为 myData,它指的是您要监视的范围内的一个对象。所以我所做的只是在 GET 请求完成后推送每一行。

完整示例来自 Here 来自 Punklr。

您可以这样更改 fieldId

$scope.gridOptions = 
{
     enableSorting: true,
     columnDefs: [
          { name: 'Id', field: 'PK_Inspection' },
          { name: 'Employee Id', field: 'employeeId' },
          { name: 'Employee Name', field: 'employeeName' },
          { name: 'Equipment Id', field: 'equipmentId' },
          { name: 'Equipment Name', field: 'equipmentName' },
          { name: 'Sequence', field: 'sequenceName' },
          { name: 'Details', field: 'sequenceDetails' },
          { name: 'Type', field: 'sequenceTypeName' },
          { name: 'Shift', field: 'shiftName' },
          { name: 'Status', field: 'statusName' }
        ],
     data:[]
};

对我来说,下面的代码有效,一旦我更改数据,我就会调用下面的代码

if ($scope.gridApi ) {
       $scope.$timeout(() => {
          $scope.gridApi.core.handleWindowResize();
          $scope.gridApi.grid.refresh();
       }, 10);
}