What is the functionality of the large arrow notation when used in conjunction with the value formatter in ag-Grid

In my Angular project, I have created a GridFormatterService service with static methods that can be used for value formatting in ag-Grid.

For certain ag-Grids in the project, I need to pass a displayDateAndTime parameter (a boolean flag) from the column definition valueFormatter to the service to determine how the value should be formatted.

However, the typical way of doing this, like:

public columnDefs = {
    ... , valueFormatter: GridFormatterService.dateFormatter(params, displayDateAndTime), ...
}

does not work. I came across a solution here that addresses this issue, so I updated the dateFormatter method as follows:

public static dateFormatter(displayDateAndTime: boolean = false) {
    return (params: ValueFormatterParams) => { 
      if (params.value) { 
        if (!displayDateAndTime) { 
          return new Date(params.value).toLocaleDateString('en-GB');
        } else { 
          return moment(params.value).format('DD/MM/YYYY HH:mm');
        }
      }
    }
  }

However, this new implementation only works when the displayDateAndTime parameter is provided. In cases where the parameter is not passed (even with a default value or allowing it to be null), the dateFormatter breaks and the code is displayed as a string in the ag-Grid. Instead of the formatted value, I see

params => if (params.value)...
in the grid.

My questions are:

  1. How does the arrow notation allow the function to access params from ag-Grid without explicitly passing it in?
  2. How can I set a default value for the flag in dateFormatter without causing issues in the ag-Grid display? If not possible, is there an alternative method for passing parameters to valueFormatter that I may be overlooking?

Thank you for your assistance!

Answer №1

I have successfully tested the following approach and it works perfectly:

columnDefs = [
...
    {
      field: 'someField',
      valueFormatter: (params: ValueFormatterParams) =>
        GridFormatterService.dateFormatter(params, true),
    },
    {
      field: 'otherField',
      valueFormatter: (params: ValueFormatterParams) =>
        GridFormatterService.dateFormatter(params),
    },
...
]

Below is the Service for reference. Please keep in mind when working with TypeScript that a property may or may not exist, use a question mark.

For example: function(a: params, b?: boolean)

    class GridFormatterService {
...
      public static dateFormatter(
        params: ValueFormatterParams,
        displayDateAndTime?: boolean
      ) {
        if (params.value) {
          if (!displayDateAndTime) {
            return 'Format A';
          } else {
            return 'Format B';
          }
        }
      }
...
    }

For a demonstration of the working example, you can visit this link: https://stackblitz.com/edit/angular-ag-grid-formatter?file=src%2Fapp%2Fapp.component.ts

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

My CSS seems to be causing issues and generating errors in the console. What could be the problem?

My CSS was working fine just 5 minutes ago, but now it's not working and I'm not sure what the issue is. I checked the console and found this error: bootstrap.min.js:6 Uncaught TypeError: Cannot read property 'fn' of undefined at bo ...

Refresh the current page in Next.js when a tab is clicked

I am currently working on a Next.js page located at /product While on the /product page, I want to be able to refresh the same page when I click on the product link in the top banner (navbar) that takes me back to /product. Is there a way to achieve this ...

What could be the reason for attempting to insert metadata into a Google Drive document resulting in it being saved as plain text

I'm working on updating the metadata of a file that was previously uploaded to Google Drive. The metadata includes the title and description. However, I've noticed that when I submit the file, the name remains unchanged. The metadata seems to be ...

Managing browser navigation within a web application

I am currently developing a web-based application for internal use in the company I work for. The application is quite intricate, featuring numerous forms that will enable users to both view and input data, which will then be stored in a database upon savi ...

What is the purpose of requiring the explicit invocation of app.listen(port) to enable express-ws to function properly?

I've recently started exploring NodeJS Express and came across the official tutorial from express-ws for setting up websockets in a simple project generated using npx express-generator. While following the tutorial, I noticed that in the app.js file, ...

Tips for customizing the CSS of a child component that has been imported from a different module in Angular 2

I have been using agm-snazzy-window from amg-snazzy-window There is a mention that I can modify the css class, but when I try to do so, it doesn't seem to work. Here is an example: map-component.html <agm-snazzy-info-window [closeWhenOthersOp ...

Tips for enhancing the color saturations of cells that overlap in an HTML table

My goal is to enhance the color of overlapping cells in my HTML tables. For instance, when I click on cell 2, the .nextAll(':lt(5)') method will change the class of the next 4 cells. https://i.stack.imgur.com/mwv8x.png Next, clicking on cell 3 ...

Is there a way to upload a file using express/multer without triggering a redirect?

Note: Despite coming across this post, I couldn't find it helpful. Other related posts were focused on angular/react, which are not relevant to my current project. I have implemented a file upload feature that should provide a response indicating whe ...

Utilizing TS2722 alongside restrictive parameters in tsconfig

I have encountered these errors due to the presence of the strictNullChecks parameter, which is activated by the strict configuration in the tsconfig.json file. It appears that when arguments.length === 2, the ab function should be available (thanks to Fun ...

execute the middleware function within the router

I've integrated sessions in my express.js application using Mozilla's client-sessions and I'm encountering an issue. My post and get requests are in router.js, while the middleware is in app.js. When I try to run it, I receive the following ...

"I am looking for a way to retrieve dynamic data from a (click) event in Angular. Can

Within my component, I have a video loaded in an i tag on a click event. The challenge is accessing the video ID from the video.component.ts file in order to dynamically load a new video. The solution has been elusive so far. <li *ngFor="let video of c ...

Building an npm module locally: A step-by-step guide

Imagine I'm in the process of working on an application called MyApp and I want to create an NPM module for it, named MyModule. Currently, I can think of two approaches to developing it: Make changes -> save -> npm install /path/to/module in M ...

JavaScript substring() function in clone is experiencing an error

I am currently working on a JavaScript function that determines whether a specific substring is present in a larger main string. For instance, if the main string is "111010" and the substring is "011," the expected result should be false since the substr ...

Avoid commencing an EmberJs application with static assets already in place

Is there a way to obtain the minified EmberJs JavaScript and CSS assets without automatically initializing the EmberJs App? The scenario is having a login.html where users can log in, but with the static assets preloaded to eliminate waiting time once red ...

Success is returned as AJAX error

My AJAX call is returning an error as Success instead of a JSON error from ASP.NET MVC. Can someone please advise on how I can resolve this issue? Thank you. [HttpPost] public JsonResult Register(int EventID) { try { ...

What is the best way to display time instead of angles in highcharts?

Hey there! I'm currently working with highcharts and I have a polar chart where I want to display time on the y-axis instead of angles. Here's what I've tried so far: On the x-axis, I have angles and I've set tickInterval: 45,. How can ...

The Bulma calendar date input fails to display the pre-filled start date

I am facing a challenge while trying to integrate the Bulma calendar into my project, particularly when it comes to setting a default start date. According to the documentation, I am using the following method: <input type="date" data-start-date="10/2 ...

Changing the selection in the Angular Mat-Select dropdown causes the dropdown's position to shift

Issue with dropdown position: The dropdown should display below the select element, but when selecting the second value, it appears on top of the select element. I have removed any relevant CSS to troubleshoot, but the problem persists. See screenshots for ...

Unexpectedly, the task of "AssignTargetPath" did not complete successfully

While working on my Node project using Node Tools for Visual Studio (version 1.1), I encountered this error unexpectedly. I was using Visual Studio Community Edition 2015 and had the latest version of Typescript (v1.7.6) installed. Error: C:\Program ...

Having trouble getting a jQuery variable to work as an argument in an onclick function for an HTML

success: function(jsonresponse) { data = JSON.stringify(jsonresponse); $.each(JSON.parse(data), function (index, item) { var Id=item.id; var JobId=item.JobId; var eachrow = "<tr>" + "<td>" + item.id + "</td>" ...