nggrid使用rest调用 - 不从$ scope填充ng-grid

时间:2014-06-21 02:44:52

标签: angularjs ng-grid

创建了一个PLNKR。请建议改变plnkr http://plnkr.co/aKbiu1QqODZTDnanhDbR

这是关于在REST调用之后nggrid不显示数据。

当我使用$ http post()和成功回调时 - 在Chrome调试窗口中,我可以看到休息调用成功并且json在控制台上被记录,并且控制台中没有其他错误 - 但仍然没有数据显示在网格/ ui上。 $ http.post()中似乎存在一些问题 - (即使我在success()方法中硬编码$ scope.myData,我也无法看到网格/ ui上填充的数据)。我在chrome中调试过,发现$ scope.mydata中有正确的数据。

如果我在$ http.post()调用之前硬编码$ scope.myData中的数据 - 我看到nggrid / ui已填充且工作正常。因此,JSON或ng-grid数据和columnDefs

没有问题

专家们可以权衡 - 谢谢。 我已经修改了下面的一些代码....

  function (controllers) {
        controllers.controller('nggridcontroller',
            [   '$scope',
                'RFLeftGridSummaryServicefunc', '$http',
                function ($scope, RFLeftGridSummaryServicefunc1, $http) {
                    // WORKS GOOD IF I HARDCODE this JSON in the $scope here outside the http call
                    //  $scope.myData  = [{"login":"kDavid1","propertyid":128105,"scenarioname":"Property History Default","year":"2014"............

                    var jsonString = '{"properties":[2323,2245]}';
                    $http({method: 'POST',
                        url: '/zzz/rf/properties/summary/?scenarioCreator=kDavid&scenarioName=budgetscenerio',
                        data: jsonString}).success(function (data) {
                        console.log('success ');
                        console.log(JSON.stringify(data)); // DISPLAYS THE DATA FROM REST CALL
                        $scope.myData = data;
                        // DOES not work EVEN if I HARDCODE the working JSON in scope
                        //$scope.myData  = [{"login":"kDavid1","propertyid":128105,"scenarioname":"Property History Default","year":"2014"......
                    }).error(function (data) {
                        console.log('error ');
                    });

                    // NG-GRID JSON
                    $scope.gridOptionsFromController = {
                        //  here is data from scope
                        data: 'myData',
                        columnDefs: [
                            {field: 'year', displayName: 'Year-fromController'},
                            {field: 'occupancy', displayName: 'Occ'} ,
                            {field: 'rent', displayName: 'Rent'} ,
                            {field: 'revenue', width: '90', cellFilter: 'currency', displayName: 'Rev'}
                        ]............

JSON:

[{"login":"jim1","propertyid":128105,"scenarioname":"Property History Default","year":"2014","rent":824,"rentyoychg":0.0430,"revenue":2313024,"revenueyoychg":0.0473,"occupancy":0.956,"occupancyyoychg":-0.0052}]                                

HTML:

   <!-- Grid - using ng-grid -->
        <div ng-controller="nggridcontroller" class="gridStyle leftMid" ng-grid="gridOptionsFromController"></div>

2 个答案:

答案 0 :(得分:0)

我认为这是在你创建“gridOptions”的时候发生的。范围内的对象没有&#39; myData&#39;属性(b / c异步http成功将被击中)所以我认为你应该定义&#39; myData&#39;开头的属性为空数组:

  function (controllers) {
    controllers.controller('nggridcontroller',
        [   '$scope',
            'RFLeftGridSummaryServicefunc', '$http',
            function ($scope, RFLeftGridSummaryServicefunc1, $http) {
                // WORKS GOOD IF I HARDCODE this JSON in the $scope here outside the http call
                //  $scope.myData  = [{"login":"kDavid1","propertyid":128105,"scenarioname":"Property History Default","year":"2014"............

                var jsonString = '{"properties":[2323,2245]}';
                $scope.myData = [];
                $http({method: 'POST',
                    url: '/ysconfig/rf/properties/summary/?scenarioCreator=kDavid&scenarioName=budgetscenerio',
                    data: jsonString}).success(function (data) {
                    console.log('success ');
                    console.log(JSON.stringify(data)); // DISPLAYS THE DATA FROM REST CALL
                    $scope.myData = data;
                    // DOES not work EVEN if I HARDCODE the working JSON in scope
                    //$scope.myData  = [{"login":"kDavid1","propertyid":128105,"scenarioname":"Property History Default","year":"2014"......
                }).error(function (data) {
                    console.log('error ');
                });

                // NG-GRID JSON
                $scope.gridOptionsFromController = {
                    //  here is data from scope
                    data: 'myData',
                    //  here is the dynamic template for a ROW - and not just html header - it has data for year etc.
                    // columndefs is a misnomer - this has the entire ROW of data here.
                    columnDefs: [
                        {field: 'year', displayName: 'Year-fromController'},
                        {field: 'occupancy', displayName: 'Occ'} ,
                        {field: 'rent', displayName: 'Rent'} ,
                        {field: 'revenue', width: '90', cellFilter: 'currency', displayName: 'Rev'}
                    ]............

答案 1 :(得分:0)

按照提到的here

进行尝试
$scope.myData =  JSON.parse(JSON.stringify(data), $scope.myData );