我在这里错过了什么?网格呈现没有错误,空行…我检查了
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,"SequenceName": "UNIT 2","SequenceDetails": "Charge,Water Levels,Vent Caps in place,Power disconnect works.","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是一个字符串,我不是100%确定我是否需要通过使用JSON.parse转换为对象然后将其传递给$scope.gridOptions.data但这可能是问题所在我在上面的原始问题中发布的代码.
>经过更多的研究,我在Angular UI Grid官方文档中找到了一个很好的深度示例.遵循这种技术,我能够正确地呈现数据.
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."); }); };
在该示例中,它使用了名为myData的gridOptions.data中的字符串值,该值引用了要观看的作用域上的对象.所以我正在做的只是在GET请求完成后推送每一行.
完整的例子是通过Punklr的Here.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。