UI-Grid如何在嵌套的JSON数组中访问绑定值

时间:2016-06-15 07:25:11

标签: angularjs json angular-ui-grid ui-grid

如果数据是嵌套的JSON数组,有人可以指导访问绑定值的正确语法。以下是用例http://plnkr.co/edit/tizLxnAtgqDnptiEb3mg?p=preview

的plunker

基本上,正如你在这里看到的地址是不同地址的集合,我需要一种方法来过滤它,类似于我在上面已经尝试过的Angular JS过滤器表达式并且它正在工作。但是有一些特定于UI Grid的表达式,例如MainAttribute [childAttribute =' XYZ']。值或类似的东西。

问题是我能够使用AngularJS表达式从数组中检索已过滤的子属性,但在后端它仍然是一个数组并导致诸如来自前端的问题无法在该列上进行过滤当您导出到CSV时,它会为该列提供完整的JSON数组。

app.js

var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.edit']);

app.controller('MainCtrl', ['$scope', function ($scope) {
    var statusTemplate = "<div>{{(address | filter:{street:'303 Dove Ave'})[0].city}}</div>";

    $scope.gridOptions = {
        enableSorting: true, 
        columnDefs: [{
            name: 'firstName',
            field: 'first-name'
        }, {
            name: '1stFriend',
            field: 'friends[0]'
        }, {
            name: 'city',
            cellTemplate: statusTemplate
        }, {
            name: 'getZip',
            field: 'getZip()',
            enableCellEdit: false
        }],
        data : [{
            "first-name": "Cox",
            "friends": [
                "friend0"
            ],
            "address": [{
                street: "301 Dove Ave",
                city: "Nairobi",
                zip: "39565"
            }, {
                street: "302 Dove Ave",
                city: "Delhi",
                zip: "39565"
            }, {
                street: "303 Dove Ave",
                city: "Patna",
                zip: "39565"
            }, {
                street: "304 Dove Ave",
                city: "Jamshedpur",
                zip: "39565"
            }, {
                street: "305 Dove Ave",
                city: "Bangalore",
                zip: "39565"
            }],
            "getZip": function() {
                return this.address.zip;
            }
        }]
    };
}]);

的index.html

<html ng-app="app">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-animate.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid-unstable.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid-unstable.css" type="text/css">
    <link rel="stylesheet" href="main.css" type="text/css">
  </head>
  <body>

<div ng-controller="MainCtrl">
  <div ui-grid="gridOptions" class="grid"></div>
</div>
    <script src="app.js"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以使用“row.entity”访问行的实体。所以只需更改包含statusTemplate的行:

var statusTemplate = "<div>{{(row.entity.address | filter:{street:'303 Dove Ave'})[0].city}}</div>";

这对我有很大的帮助: http://brianhann.com/6-ways-to-take-control-of-how-your-ui-grid-data-is-displayed/