Results:
Loading...

Angular Data GridValue Formatters

Value formatters allow you to format values for display. This is useful when data is one type (e.g. numeric) but needs to be converted for human reading (e.g. putting in currency symbols and number formatting).

Below shows the column definition properties for value formatters.

valueFormatter
string | ValueFormatterFunc
A function or expression to format a value, should return a string. Not used for CSV export or copy to clipboard, only for UI cell rendering.
valueFormatter: string | ValueFormatterFunc<TData>;

interface ValueFormatterFunc<TData = any> {
    (params: ValueFormatterParams<TData>) : string
}

interface ValueFormatterParams<TData = any, TValue = any> {
  // Value for the cell. 
  value: TValue;
  // Row node for the given row 
  node: IRowNode<TData> | null;
  // Data associated with the node 
  data: TData | undefined;
  // Column for this callback 
  column: Column;
  // ColDef provided for this column 
  colDef: ColDef<TData>;
  // The grid api. 
  api: GridApi<TData>;
  // The column api. 
  columnApi: ColumnApi;
  // Application context as set on `gridOptions.context`. 
  context: any;
}
// example value formatter, simple currency formatter
colDef.valueFormatter = params => {
    return '£' + params.value;
}

Value Formatter vs Cell Renderer

A cell renderer allows you to put whatever HTML you want into a cell. This sounds like value formatters and a cell renderers have cross purposes, so you may be wondering, when do you use each one and not the other?

The answer is that value formatters are for text formatting and cell renderers are for when you want to include HTML markup and potentially functionality to the cell. So for example, if you want to put punctuation into a value, use a value formatter, but if you want to put buttons or HTML links use a cell renderer. It is possible to use a combination of both, in which case the result of the value formatter will be passed to the cell renderer.

Be aware that the Value Formatter params won't always have 'data' and 'node' supplied, e.g. the params supplied to the Value Formatter in the Set Filter. As a result favour formatter implementations that rely upon the 'value' argument instead, as this will lead to better reuse of your Value Formatters.

Value Formatter Example

The example below shows value formatters in action.

  • Columns A and B display the value of the field property
  • Columns £A and £B use a currencyFormatter to display the value as a currency
  • Columns(A) and (B) use a bracketsFormatter to display the value inside brackets