本文介绍了Angular Grid ag-grid columnDefs 动态变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 columnDefs 动态变化的问题.这是我的 gridOptions:

I have a problem about columnDefs change dynamically. Here is my gridOptions:

$scope.gridOptions = {
  columnDefs: [],
  enableFilter: true,
  rowData: null,
  rowSelection: 'multiple',
  rowDeselection: true
};

当我从服务器检索数据时:

And when I retrieve data from server:

$scope.customColumns = [];

$http.post('/Home/GetProducts', { tableName: 'TABLE_PRODUCT' }).success(function (data) {
    angular.forEach(data.Columns, function (c) {
        $scope.customColumns.push(
            {
                headerName: c.Name,
                field: c.Value,
                width: c.Width
            }
        );
    });

    $scope.gridOptions.columnDefs = $scope.customColumns;

    $scope.gridOptions.rowData = data.Products;
    $scope.gridOptions.api.onNewRows();
}).error(function () {

});

注意:这里的 c 是来自服务器的列对象.

当动态生成列并将其分配给 $scope.gridOptions.columnDefs 时,有空白网格,但 $scope.customColumns 数组填充了正确生成的列对象.请帮助我是这个错误还是我做错了什么?

When dynamically generating columns and assigning it to $scope.gridOptions.columnDefs there is blank grid but $scope.customColumns array is filled with right generated column objects. Please help me is this bug or I am doing something wrong?

推荐答案

在 ag-grid 中,gridOptions 中的列在网格初始化时使用一次.如果在初始化后更改列,则必须告诉网格.这是通过调用 gridOptions.api.setColumnDefs()

In ag-grid the columns in gridOptions are used once at grid initialisation. If you change the columns after initialisation, you must tell the grid. This is done by calling gridOptions.api.setColumnDefs()

此 api 方法的详细信息在 ag-grid 文档中提供.

Details of this api method are provided in the ag-grid documentation here.

这篇关于Angular Grid ag-grid columnDefs 动态变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 03:49