AngularJS ng-grid Phone format

484 Views Asked by At

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} 
        ]};
2

There are 2 best solutions below

0
sathish_at_madison On BEST ANSWER

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>
1
Clark Roberts On

Replace the }, at the end of the phonenumber field with the following

, cellTemplate: " ({{row.getProperty(col.field).substr(0, 3)}}) {{row.getProperty(col.field).substr(3, 3)}} - {{row.getProperty(col.field).substr(6,4)}}"