Formatting phone numbers in AngularJS using ng-grid

Curious if anyone has come across this issue while using the ng-grid component to display data.

I have a column with phone numbers and I'm wondering if there is a simple way to format them as (123)-123-1234.

This is what my ng-grid code currently looks like:

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

Answer №1

Substitute the }, located at the conclusion of the telephone field, with the subsequent

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

Answer №2

I managed to find a solution that actually worked. When working with the JSON string, I came across the information "row.getProperty(col.field)|phoneNumber". In the controller code, it looks something like this for the specific 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, you will find the following snippet of code.

<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Slider MIA - Where Did it Go?

I'm having an issue with my slider. When I load the page, it doesn't show the first image until I click the button to load it. How can I make it display the first image by default? Below is my JavaScript code: <script> var slideIndex = 1 ...

What is the best way to reference a nested jQuery variable, whether global or local, within an HTML document

I've been working on this problem for days with no success. Any help would be greatly appreciated. I'm trying to call a variable from nested jQuery into HTML. The variable is named "PROBLEM". Even after trying to make it global, any updates made ...

Guide to utilizing a web worker from an npm package in your primary webpack application

My project is structured in the following way: moduleA -> Library that I publish to npm using Rollup projectA -> My Gatsby project that installs moduleA and utilizes it. To bundle my workers with other library code into the dist folder, I am using ...

Discovering the ways to retrieve Axios response within a SweetAlert2 confirmation dialog

I'm struggling to grasp promises completely even after reviewing https://gist.github.com/domenic/3889970. I am trying to retrieve the response from axios within a sweetalert confirmation dialog result. Here is my current code: axios .post("/post ...

How can Node.js identify the page where visitors entered, the page they are currently on, and the page

After creating a Node server with socket.io, I'm interested in tracking the entry, current, and exit pages for each user. My plan is to store this information within the user's session for future reference. I'm currently stuck on how to obt ...

SimulatedGreg encountered an error message with Electron-Vue-Vuetify: [Vue warn]: The custom element <v-app> is not recognized - have you properly registered the component?

Greetings, I extend my gratitude for extending your assistance towards resolving my current predicament. The issue at hand arose early this morning around 9 am and despite hours of troubleshooting up to 3 pm, the solution continues to evade me. Countless Y ...

Is there a way to track and observe the vertical movement of an element within a directive's link function?

Can you help me figure out the best way to $watch or bind the height position of an element? I have a scenario where I display three divs, one at a time in different tabs (using bootstrap). Each div has a different height, and my directive comes after thes ...

Draggable resizing of the Accordion component in React.js using Material-UI

In the visual representation, there are two Accordions—one positioned on the left (30%) and the other on the right (70%). Upon clicking the button, the right accordion disappears, while the one on the left expands to cover the full width (100%). A featu ...

The Checkbox component from Material-UI does not seem to be compatible with the Redux

The data source can be found in this repository Although I am using Redux store to update the checkbox's check flag and observing that the state changes correctly, unfortunately, these modifications are not reflecting on the React components. It see ...

Loading content from another webpage within a webpage using Ajax

I successfully implemented an Ajax chat in a PHP page and it was working perfectly. However, when I tried to integrate the chat into another page using a pop-up div (via CSS with z-index), I encountered some issues. Here is the code snippet: function sho ...

Tips for maintaining user sessions in Passport.js and PhoneGap: "remembering" a user after logging in

My Node.js server runs on the Sails.js framework, and I've successfully integrated passport.js to handle authentication. Here's how it works: (login)POST /auth/local: Validates credentials and returns ID, Username, and Email address. (register) ...

Struggling with sending form data to the back end when uploading images in Angular

I've been facing a challenge trying to implement profile picture upload alongside standard text data and sending it all to the backend to create a new user through mongoose. Despite my efforts, using tools like ng-file-upload/angular-file-upload and e ...

Universal - Permissible data types for function and constructor arguments

In many statically typed languages, it is common to specify a single type for a function or constructor parameter. For example: function greet(name: string) { ... } greet("Alice") // works greet(42) // error TypeScript is an extension of JavaScri ...

Adding and adjusting the size of different DIV elements within a shared area

Looking to create a dynamic bar with the ability to add multiple child DIVs (similar to this: https://i.stack.imgur.com/tdWsq.jpg). Utilizing jQuery, jQuery UI, Bootstrap, and various plugins. The structure for the div (or span) system could be structured ...

Link AngularJS service array length property to the controller's scope through binding

I am attempting to connect the length of an array from another service to my controller's scope in the following manner: app.controller('TestCtrl', function ($scope, safePostService) { $scope.count = safePostService.queue.length; $ ...

Having difficulty transferring data from a JSON file on a different domain to a variable using Ajax

As a beginner in Ajax, I am currently exploring the use of Ajax Cross domain functionality to fetch data. The Ajax function is triggered from test.php, which then calls stats.php to request the desired data. The content of Stats.php: <?php $data = ...

Issue with AngularJS ng-if causing hidden elements to not respond to ng-select event

In my code, there is a hidden select input that is initially hidden using ng-if. Inside this hidden select, I have included a ng-change tag, which triggers a function to display an img element that is also hidden by ng-if. The element hiddenNinja is hidd ...

Listening to audio on a mobile browser using an HTML audio element

What is the solution for the problem of playing audio on a mobile browser when it plays on a desktop browser but not on mobile? The code being used is: var audio = new Audio('sound.mp4') audio.play() ...

The NextJS App directory consistently stores the retrieved data in the cache, regardless of any no-cache options that may have been configured

Despite trying various online advice, I am still facing the issue of cached fetched data. I am using a mongodb server, and even though I add data to the server, I can only see the new data on the server itself, not on the website (before the NextJS project ...

Using logic to eliminate elements

During the concluding ceremony, I have a plan in mind: In case there is only one input field: Do nothing If multiple input fields exist and none of them are empty: Do nothing For multiple input fields where at least one field has content: Remove all ...