将数据值传递给ui-grid

时间:2015-10-20 05:16:34

标签: html angularjs angularjs-scope

我想传递数据值ui-grid。我需要将$ scope.ll值传递给ui-grid。如果我复制数据并分配$ scope.ll = [{}] Id和statesum_totalcount运行良好。我需要将$ scope.ll传递给grid

我在数组中的数据是

{
"ID": "3",
"stat_sum": {
    "totcount": 3
},
"zip_stats": [
    {
        "zip": "560045",
        "count": 1
    },
    {
        "zip": "567657",
        "count": 2
    }
],
"qual_stats": [
    {
        "count": 1,
        "qualification": "B.E."
    },
    {
        "count": 2,
        "qualification": "BE"
    }
],
"prof_stats": [
    {
        "count": 1,
        "profession": "Doctor"
    },
    {
        "count": 2,
        "profession": "Software Engineer"
    }
],
"city_stats": [
    {
        "city": null,
        "count": 2
    },
    {
        "city": "Bangalore",
        "count": 1
    }
],
"state_stats": [
    {
        "count": 1,
        "state": "Karnataka"
    },
    {
        "count": 2,
        "state": "Kerala"
    }
],
"stats_info": [
    {
        "acount": 3,
        "answer": "fgdfgd",
        "question": "comment-about-me-"
    },
    {
        "acount": 1,
        "answer": "one",
        "question": "radio-answer-"
    },
    {
        "acount": 2,
        "answer": "two",
        "question": "radio-answer-"
    },
    {
        "acount": 3,
        "answer": "t-shirt",
        "question": "select-any-dress-for-me-[]"
    },
    {
        "acount": 3,
        "answer": "no",
        "question": "say-yes-or-no-"
    },
    {
        "acount": 3,
        "answer": "2015-09-25",
        "question": "select-your-b.date-"
    },
    {
        "acount": 3,
        "answer": "24",
        "question": "select-your-age-"
    },
    {
        "acount": 3,
        "answer": "2",
        "question": "type-number-"
    },
    {
        "acount": 3,
        "answer": "false",
        "question": "select-true-or-false-"
    }
]
}

在控制器中

 $timeout(function () {
     console.log($scope.ll);    //works fine
        $rootScope.showspinner = false;
        $scope.gridOptionsComplex = {
            enableFiltering: true,
            showGridFooter: true,
            showColumnFooter: true,
            columnDefs: [

                {name: 'ID', width: 100, enableCellEdit: false,},
                {name: 'stat_sum.totcount', width: 100, enableCellEdit: false,},
                {name: 'zip_stats.zip', width: 100, enableCellEdit: false,},
                {name: 'zip_stats.count', width: 100, enableCellEdit: false,},
                {name: 'qual_stats.qualification', width: 100, enableCellEdit: false,},
                {name: 'qual_stats.count', width: 100, enableCellEdit: false,},
            ],

     data:$scope.ll
        };
        $scope.$apply(function () {
            $scope.aut = true;
        });
    }, 500, false);

1 个答案:

答案 0 :(得分:0)

我面临同样的问题,我给出了一个示例代码如何解决这个问题

{
"result": {
    "fileNames": [
        "Book1 (2).csv",
        "address_sample (3).csv",
        "Book1.csv"
    ],
    "ids": [
        1,
        2,
        3
    ]
},
"responseSuccess": "success",
"responseError": null,
"responseInfo": null,
"responseWarning": null,
"responseCode": 0
}

我的输出与上面的格式一样,我也必须将这些输入与ui-grid集成。

示例控制器端代码一旦控件达到成功部分我将输出作为上面的格式,所以让我们看看如何与ui-grid集成,

在我的控制器中,我有$scope.gridsOptions喜欢

$scope.gridsOptions = {
columnDefs : [
    {
        field : 'field',
        name : 'Id'
    },
    {
        field : 'filename',
        name : 'FileName'
    }]
 }

我认为控件来到了成功部分.success(function()),有什么验证可以请你验证,

.success(function(data){
$scope.resultValues =[];

// Here My PD(Problem Domain says iterate based On `ids`)
for (var i = 0; i < data.result.ids.length; i++) {

       // Im getting each ID in  $scope.fileId variable
       $scope.fileId = data.result.ids[i];

      // Here Im getting each fieldNames in  $scope.fileName variable 
      $scope.fileName = data.result.fileNames[i];

      //Then I am pushing those values in to fieldId
      $scope.resultValues.push({
                        field : $scope.fileId,
                        filename : $scope.fileName
                });
      }

      ListData.fileIdValues = angular.copy($scope.resultValues);

      // I am pointed  those to $scope.gridsOptions.data
      $scope.gridsOptions.data = ListData.fileIdValues;
})                                                          

请在我的代码中注意 ListData.fileIdValues,它是有角度的服务变量this.fileIdValues ={}。对于我的问题域,我需要这些值,因此我将其存储在service中并在任何我想要的地方使用它。