【问题标题】:passing data value to ui-grid将数据值传递给 ui-grid
【发布时间】:2015-10-20 15:04:44
【问题描述】:

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

我的数组中的数据为

{
"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);

【问题讨论】:

    标签: html angularjs angularjs-scope


    【解决方案1】:

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

    {
    "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 并在我想要的任何地方使用它。

    【讨论】:

    • 您能解释一下吗?
    • 请问是哪一部分?
    • 如何修改 columnDefs : [] 到我的数组值先生以及如何使用 data.result.ids.length
    • 您好,我的英语也不流利。所以我以为你在问如何使用 data.result.ids.lenght 。我说的对吗?
    • 是的,我还需要在 colDef[] 中添加字段
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    相关资源
    最近更新 更多