关于angularjs:setRowData不会将数据加载到网格中

setRowData not loading the data in to the grid

我正在使用ag-grid在网格中显示值。但是使用setRowData不会将值加载到网格中。未定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<html>
 <head>
 </head>
 <body ng-app ng-controller="CustController as custCtrl">
     <div ag-grid="custCtrl.gridOptions" id="myGrid" style="height:
      115px;width:500px;" class="ag-fresh">
 </body>
</html>

var app = angular.module('myApp');
app.controller('CustController', function($scope, $http) {
var columnDefs = [
  {headerName:"Customer Name", field:"custName"},
  {headerName:"Address", field:"address"},
  {headerName:"Ph NO", field:"phNo"}
];

var rowData = [
{custName:"A", address:"xyz", phNo: '123-123-1234'},
{custName:"B", address:"lmn", phNo: '345-456-5672'},
{custName:"C", address:"pqr", phNo: '903-456-2345'}
];

var gridOptions = {
  columnDefs: columnDefs,
  enableColResize: true,
  rowBuffer:0,
  enableSorting: true,
  enableFilter: true,
  rowHeight: 30,
  headerHeight: 35,
  sizeColumnsToFit: true,
  onGridReady: function () {
    setTimeout( function(){
                        $scope.gridOptions.api.setRowData(rowData);
                    },5000);
   },
   suppressLoadingOverlay: true,
   pagination: true
 };
});

尝试将onGridReady函数移到gridOptions之外。

1
2
3
4
5
var rowData = [
{custName:"A", address:"xyz", phNo: '123-123-1234'},
{custName:"B", address:"lmn", phNo: '345-456-5672'},
{custName:"C", address:"pqr", phNo: '903-456-2345'}
];

通过单击getData()函数的按钮单击时通过http get方法从数据库中获取

1
2
3
4
5
6
7
$scope.getData = function(){
  //console.log("hello");
  $http.get("https://raw.githubusercontent.com/ag-grid/ag-grid-docs/master/src/olympicWinners.json")
    .then(function(res){
        $scope.gridOptions.api.setRowData(res.data);
    });
}

https://plnkr.co/edit/PKTiFpd9WM1UeELT72ht?p=preview


您的$ scope中未定义您的gridOptions变量,但是您尝试通过onGridReady函数中的作用域访问它。

尝试一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$scope.gridOptions = {
  columnDefs: columnDefs,
  enableColResize: true,
  rowBuffer:0,
  enableSorting: true,
  enableFilter: true,
  rowHeight: 30,
  headerHeight: 35,
  sizeColumnsToFit: true,
  onGridReady: function () {
    setTimeout( function(){
                        $scope.gridOptions.api.setRowData(rowData);
                    },5000);
   },
   suppressLoadingOverlay: true,
   pagination: true
 };
});