Hi,
Is there a built in support to parse and format columns as numbers or currency when they have numeric values as string, i.e. within single quotes?
Like, if I have the following Grid:
It works fine when the data is:
But it does not display anything when the data is as follows - i.e. Score in single quotes:
I wasn't sure if there is a built in support for this, or I just write a custom formatter for this. 'format: N2' does not work for it.
Thanks!
Hi Tera P Keplinger,
Thanks for contacting Syncfusion support.
By default, to apply a number formatting to a column, the values must be a number. So, you need to iterate the data array to convert the string value to a number before setting it to the Grid since there is no built-in support for this. Or you can use the column “valueAccessor” property to set the format in a custom way. However, the other data actions for this column like filtering, sorting, and grouping will be done based on the original string value only. Please refer to the below code example, documentation, and sample link for more information.
|
var intl = new ej.base.Internationalization();
function numberFormatter(field, data, column) { var nParser = intl.getNumberParser({ format: 'N3' }); return nParser(data[field]); }
var grid = new ej.grids.Grid({ . . . columns: [ { field: 'Score', headerText: 'Score', valueAccessor: numberFormatter, }, ], }); grid.appendTo('#Grid');
|
Documentation: https://ej2.syncfusion.com/javascript/documentation/grid/columns/columns/#valueaccessor
https://ej2.syncfusion.com/javascript/documentation/common/internationalization/#number-formatting
Sample : https://stackblitz.com/edit/396mzc?file=index.js
Please get back to us if you need further assistance on this.
Regards,
Pavithra S
Hi Pavithra,
Thank you for your response. This is pretty much what I had tried but there's a difference that I see between 'valueAccessor' attribute, and the 'format' attribute itself for the Column with the filters ON.
If I use 'format: N2' in a column with numeric data, this is how the filter options look like - they all have 2 digit options.
But when I use the valueAccessor option with the string data as below:
The grid displays the formatted values but the filter options show the original unformatted values like below:
(Also, 14.2 shows as 14.2 and not 14.20 on grid even with format:N2)
Is there a way to handle this, and have filter values show up as defined by the format (same way as it works with 'format' option of the column) ?
Thanks!
Hi Tera P Keplinger,
Since the “valueAccessor” property is only for displaying the customized cell value, it will not affect the filtering. As we said earlier, the Grid data actions will be done based on the original string values as in the dataSource only. So, if you want to do the number filtering the column value should be a number in Grid dataSource.
Regards,
Pavithra S
Hi Pavithra,
Thank you for your response. I'll check what can be done with the data to recieve this field as a number itself.
Thanks!
Hi Tera P Keplinger,
Welcome! Please get back to us if you need further assistance.
Regards,
Pavithra S