setRowData没有将数据加载到网格中

问题描述 投票:0回答:2

我正在使用ag-grid将值显示到grid.But使用setRowData没有将值加载到grid.Below是我的代码,我在setTimeOut函数中调用setRowData,因为它抛出api是未定义的:

<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"></div>
 </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
 };
});
angularjs ag-grid
2个回答
1
投票

尝试在gridOptions之外移动onGridReady函数。

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'}
];

从数据库通过http get方法点击按钮调用getData()函数

$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


0
投票

你的gridOptions变量没有在$ scope上定义,但是你试图通过onGridReady函数中的作用域来访问它。

试试这个:

$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
 };
});
© www.soinside.com 2019 - 2024. All rights reserved.