0

我有要求,例如用户将单击任何表名,然后我必须在 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);
};

根据表名,我必须在网格中动态加载列和记录。但它总是在说“正在加载”并且没有要渲染的东西。

如果有任何示例,请参考或重构我的代码。

4

1 回答 1

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 )
    })
};
于 2016-02-22T13:51:52.880 回答