如何使用 ag-grid 加载动态列和行?
How to load dynamic column and rows by Using ag-grid?
Html代码:
<div class="panel-body" style="padding-bottom:0px">
<div id="myGrid" ag-grid="gridOptions" class="ag-fresh" style="height: 100%;"></div>
</div>
Angular代码:
var sqdtApp = angular.module("sqdtApp", ['ngTouch',
'ui.grid', 'ui.grid.pagination', 'ui.grid.resizeColumns',
'angularUtils.directives.dirPagination', 'ngAnimate', 'ui.bootstrap', 'agGrid']);
sqdtApp.controller(
'importedtableCtrl',
function ($http, $scope, $stateParams,$rootScope, $httpParamSerializer, uiGridConstants) {
$scope.dbtype = $stateParams.dbname;
$scope.columns = [];
$scope.gridOptions = {
columnDefs: [],
enableFilter: true,
rowData: [],
rowSelection: 'multiple',
rowDeselection: true
};
$scope.customColumns = [];
$http.post($scope.url + "/importedtablesCount", { 'dbname': $stateParams.dbname })
.success(
function (result) {
$scope.importedTableCount = result;
});
var gridtablename = "";
$scope.currentImportedTableName = '';
$scope.loadTableInGrid = function (tablename) {
$scope.currentImportedTableName = tablename;
if (gridtablename != tablename) {
$scope.reset();
gridpageno = 1;
$http.post($scope.url + "/getPagingRecordImportedTable", { 'dbname': $stateParams.dbname, 'tableName': tablename, 'pageNumber': 1 }).success(
function (response) {
$scope.names = response.records;
$scope.mdata = response.metadata;
// $scope.gridOptions.data = response.records;
var columnsize = 0;
console.log($scope.customColumns);
for (var obj in $scope.mdata) {
if ($scope.mdata[obj]['columnsize'] > 20) {
columnsize = 20;
} else {
columnsize = $scope.mdata[obj]['columnsize'];
}
$scope.customColumns.push({
headerName: $scope.mdata[obj]['columnname'],
field: $scope.mdata[obj]['columnname'],
headerClass: 'grid-halign-left'
});
}
$scope.gridOptions.columnDefs = $scope.customColumns;
$scope.gridOptions.rowData = $scope.names;
gridtablename = tablename;
gridpageno = 1;
$scope.getTotalNoOfRecordCountForGrid(tablename);
}).error(function (data) {
alert(data);
});
} else {
$scope.reset();
$scope.resetGridTableName();
}
};
});
输出没有要显示的行
no rows to show is output
但是如果我签入 $scope.gridoptions
对象,所有的行和列都在那里。
$scope.gridOptions object in console with data
但它没有在页面中呈现。
帮帮我。
$scope.gridOptions.columnDefs = $scope.customColumns;
$scope.gridOptions.rowData = $scope.names;
这些很可能是罪魁祸首:columnsDefs 和 rowData 仅用于就绪事件之前的网格初始化。
初始化后使用gridOptions.api.setColumnDefs和gridOptions.api.setRowData与网格交互
Html代码:
<div class="panel-body" style="padding-bottom:0px">
<div id="myGrid" ag-grid="gridOptions" class="ag-fresh" style="height: 100%;"></div>
</div>
Angular代码:
var sqdtApp = angular.module("sqdtApp", ['ngTouch',
'ui.grid', 'ui.grid.pagination', 'ui.grid.resizeColumns',
'angularUtils.directives.dirPagination', 'ngAnimate', 'ui.bootstrap', 'agGrid']);
sqdtApp.controller(
'importedtableCtrl',
function ($http, $scope, $stateParams,$rootScope, $httpParamSerializer, uiGridConstants) {
$scope.dbtype = $stateParams.dbname;
$scope.columns = [];
$scope.gridOptions = {
columnDefs: [],
enableFilter: true,
rowData: [],
rowSelection: 'multiple',
rowDeselection: true
};
$scope.customColumns = [];
$http.post($scope.url + "/importedtablesCount", { 'dbname': $stateParams.dbname })
.success(
function (result) {
$scope.importedTableCount = result;
});
var gridtablename = "";
$scope.currentImportedTableName = '';
$scope.loadTableInGrid = function (tablename) {
$scope.currentImportedTableName = tablename;
if (gridtablename != tablename) {
$scope.reset();
gridpageno = 1;
$http.post($scope.url + "/getPagingRecordImportedTable", { 'dbname': $stateParams.dbname, 'tableName': tablename, 'pageNumber': 1 }).success(
function (response) {
$scope.names = response.records;
$scope.mdata = response.metadata;
// $scope.gridOptions.data = response.records;
var columnsize = 0;
console.log($scope.customColumns);
for (var obj in $scope.mdata) {
if ($scope.mdata[obj]['columnsize'] > 20) {
columnsize = 20;
} else {
columnsize = $scope.mdata[obj]['columnsize'];
}
$scope.customColumns.push({
headerName: $scope.mdata[obj]['columnname'],
field: $scope.mdata[obj]['columnname'],
headerClass: 'grid-halign-left'
});
}
$scope.gridOptions.columnDefs = $scope.customColumns;
$scope.gridOptions.rowData = $scope.names;
gridtablename = tablename;
gridpageno = 1;
$scope.getTotalNoOfRecordCountForGrid(tablename);
}).error(function (data) {
alert(data);
});
} else {
$scope.reset();
$scope.resetGridTableName();
}
};
});
输出没有要显示的行
no rows to show is output
但是如果我签入 $scope.gridoptions
对象,所有的行和列都在那里。
$scope.gridOptions object in console with data
但它没有在页面中呈现。 帮帮我。
$scope.gridOptions.columnDefs = $scope.customColumns;
$scope.gridOptions.rowData = $scope.names;
这些很可能是罪魁祸首:columnsDefs 和 rowData 仅用于就绪事件之前的网格初始化。
初始化后使用gridOptions.api.setColumnDefs和gridOptions.api.setRowData与网格交互