Search code examples
javascriptangularjsng-gridphonejs

AngularJS ng-grid Phone format


Hoping this would be something someone would have encountered, using the ng-grid component for displaying data.

One of the column is a phone number. Is there an easier way to format phone as (123)-123-1234.

The ng-grid code looks like this,

 $scope.gridOptions = { data:'records',
        enableRowSelection:false,
        filterOptions: $scope.filterOptions,
        columnDefs:[
            {field:'phoneNumber', displayName:'Phone', width:80} 
        ]};

Solution

  • Here is one solution that worked. the information "row.getProperty(col.field)|phoneNumber", "phoneNumber " is coming in the JSON string. The code in controller goes something like this for the column

     $scope.gridOptions = { data:'records',
        enableRowSelection:false,
        filterOptions: $scope.filterOptions,
        columnDefs:[
            {field:'phoneNumber', displayName:'Phone', width:80,cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{row.getProperty(col.field)|phoneNumber}}</span></div>'}} 
        ]};
    

    In the html file the following code snippet.

    <div id="controller" data-ng-controller="controller">
        <div class="gridStyle" ng-grid="gridOptions" width="100%"></div>
    </div>
     <script type="text/javascript" src="/common/PhoneFormat.js"></script> 
     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js></script> 
     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-resource.js"></script>
     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-touch.js"></script>
     <script type="text/javascript" src="/common/ng-grid.js"></script>