【问题标题】:ag-grid not showing any dataag-grid 没有显示任何数据
【发布时间】:2017-06-28 20:21:51
【问题描述】:

我正在使用带有 angularjs 的 ag-grid。所以在控制器中,我用 sql DB 源填充网格行。为此,我正在进行 webapi 调用,它返回对象数组。以下是代码。

var module = angular.module("crud", ["agGrid"]);

module.controller("crudCtrl", function ($scope, $http) {
var columnDefs = [
   { headerName: "Roll No", field: "RollNo", editable: true },
   { headerName: "Name", field: "Name", editable: true },
   { headerName: "Place", field: "PlaceName", editable: true },
   { headerName: "Birth Date", field: "dob", editable: true }
];


$scope.gridOptions = {
    columnDefs: columnDefs,
    rowData: [],
    headerHeight: 42,
    rowHeight: 32

};

$http.get("api/Student/GetAllStudents").then(function (response) {
    $scope.gridOptions.rowData = response.data;

}, function (error) {
    console.log('Oops! Something went wrong while saving the data.')
});


});

但是当我运行该页面时,它没有显示任何数据。当我调试并看到它返回 response.data 中的记录作为对象数组作为数组 [12]。但它在网格中显示不一样。如果不是 response.data 我分配我自己的数组,类似于响应返回的内容,那么它会呈现数据。那么,问题出在哪里?

【问题讨论】:

    标签: angularjs asp.net-web-api rendering ag-grid


    【解决方案1】:

    如果数据来自服务器,则需要在gridOptions中将rowModelType配置为'serverSide',否则将rowModelType配置为''用于从本地加载数据。

    【讨论】:

    • 如果您使用的是一次仅将一部分数据加载到网格中的行模型,则只需将 rowModelType 设置为 serverSide。即所有数据都保存在服务器中,当网格滚动/被过滤时,网格只请求下一组行。默认情况下,正如上述问题所要求的,在所有数据都提供给 Grid 的地方使用 ClientSide 行模型。
    【解决方案2】:

    我们遇到了类似的问题。您可能需要使用 gridOptions.onGridReady

            onGridReady: () => {
                //setup first yourColumnDefs and yourGridData
    
                //now use the api to set the columnDefs and rowData
                this.gridOptions.api.setColumnDefs(yourColumnDefs);
                this.gridOptions.api.setRowData(yourGridData);
            },
    

    【讨论】:

    • thanx... 但在我的情况下它得到了 $scope.gridOptions.api.setRowData(res.data);
    • 添加数据和用新数组替换数据可能有所不同。我在收到新数据后添加了 setRowData() 调用,而不是在 onGridReady 函数中。
    猜你喜欢
    • 2018-08-07
    • 2020-07-12
    • 2016-08-02
    • 2017-09-16
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2021-10-09
    • 2016-11-22
    相关资源
    最近更新 更多