Tink Tink - 3 months ago 55
AngularJS Question

AngularJS : ui-grid field with a concatenation of two fields

I need a column in ui-grid which is a concatenation of two fields,say 'name' and 'address'. How can I achieve this?

Answer

Did you try this approach?

controller

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [
             {name: "Moroni", address: test1},
                     {name: "Tiancum", address: test2},
                         {name: "Jacob", address: test3},
                         {name: "Nephi", address: test4},
                         {name: "Enos", address: test5 }];

        angular.forEach($scope.myData,function(row){
          row.getNameAndAddress = function(){
            return this.name + '-' + this.address;
          }
        });

    $scope.gridOptions = { 
        data: 'myData',
        columnDefs: [{field: 'name', displayName: 'Name'},
                     {field:'address', displayName:'Address'},
                     {field: 'getNameAndAddress()', displayName: 'Name and address'},
                     ]
        };
});

html

<body ng-controller="MyCtrl">
        <div class="gridStyle" ng-grid="gridOptions"></div>
    </body>