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:
- How does the arrow notation allow the function to access
params
from ag-Grid without explicitly passing it in? - 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 tovalueFormatter
that I may be overlooking?
Thank you for your assistance!