我有要求用户会点击任何表名,然后我必须在ag-grid中呈现相应的数据。
我正在使用角度js 1.x。
我尝试了所有的方式
$scope.gridOptions = {};
$scope.loadTableInGrid = function (tablename) {
$http.post($scope.url + "/getPagingRecordImportedTable", { 'dbname': $stateParams.dbname, 'tableName': tablename, 'pageNumber': 1 }).success(
function (response) {
$scope.names = response.records;
$scope.mdata = response.metadata;
console.log(response.records);
console.log(response.metadata);
var columnsize = 0;
for (var obj in $scope.mdata) {
if ($scope.mdata[obj]['columnsize'] > 20) {
columnsize = 20;
} else {
columnsize = $scope.mdata[obj]['columnsize'];
}
$scope.columnDefs.push({
"headerName": $scope.mdata[obj]['columnname'],
"field": $scope.mdata[obj]['columnname']
});
}
var gridDiv = document.querySelector('#myGrid');
alert(gridDiv);
new agGrid.Grid(gridDiv, $scope.gridOptions);
$scope.gridOptions.api.setColumnDefs( $scope.columnDefs);
$scope.gridOptions.api.setRowData(response.records);
$scope.gridOptions.api.refreshView();
console.log("-----------Data received");
}).error(function (data) {
alert(data);
});
} else {
$scope.reset();
$scope.resetGridTableName();
}
console.log(tablename);
};
根据表名,我必须加载列并在网格中动态记录。 但它总是说" loading"什么都没有渲染。
如果有任何示例,请参考或重构我的代码。
答案 0 :(得分:0)
我可以在你的代码中看到1 if和2 else语句......你遗漏了什么......?
总之。 Angular ajax方法返回一个Promise,因此它应该与.then / .catch一起使用,而不是.success / .error
我会写得更像这样:(未经测试)(提防ES6箭头fn和const)
$scope.gridOptions = {};
$scope.loadTableInGrid = tablename => {
const url = $scope.url + "/getPagingRecordImportedTable"
const formData = {
dbname: $stateParams.dbname,
tableName: tablename,
pageNumber: 1
}
console.log(tablename);
$http.post(url, formData)
.then( response => {
$scope.names = response.records;
$scope.mdata = response.metadata;
console.log(response.records);
console.log(response.metadata);
var columnsize = 0;
for (var obj in $scope.mdata) {
if ($scope.mdata[obj]['columnsize'] > 20) {
columnsize = 20;
} else {
columnsize = $scope.mdata[obj]['columnsize'];
}
$scope.columnDefs.push({
headerName: $scope.mdata[obj]['columnname'],
field: $scope.mdata[obj]['columnname']
});
}
var gridDiv = document.querySelector('#myGrid');
alert(gridDiv);
new agGrid.Grid(gridDiv, $scope.gridOptions);
$scope.gridOptions.api.setColumnDefs( $scope.columnDefs]);
$scope.gridOptions.api.setRowData(response.records);
$scope.gridOptions.api.refreshView();
console.log("-----------Data received");
}).catch( err => {
console.log( "Error: ", err )
})
};