Ag-Grid:数字格式,例如:123456.78至123,457 [英] Ag-Grid: Number Formatting eg:123456.78 to 123,457
问题描述
我有大量的数字数据。
这需要呈现为逗号分隔的值。
例如
123456.78
使用Ag-Grid渲染为 123,457
。
请帮助我实现这一目标。
I have huge sets of numeric data.
this needs to be rendered as comma separated value.
For Ex.
123456.78
to be rendered as 123,457
using Ag-Grid.
Kindly help me on achieving this.
推荐答案
根据单元格渲染流程文档(此处),则可以使用colDef.valueFormatter,如下所示:
As per the cell rendering flow documentation (here), you can use the colDef.valueFormatter, like this:
var columnDefs = [
{headerName: "Number", field: "number"},
{headerName: "Formatted", field: "number", valueFormatter: currencyFormatter}
];
function currencyFormatter(params) {
return '£' + formatNumber(params.value);
}
function formatNumber(number) {
// this puts commas into the number eg 1000 goes to 1,000,
// i pulled this from stack overflow, i have no idea how it works
return Math.floor(number).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
您也可以使用cellRenderer,如其他文章所述,但这通常用于复杂的呈现,而valueFormatter专门用于这种情况。从ag-grid 文档:
You could also use a cellRenderer as other posts describe, but that's typically for more complex rendering, whereas the valueFormatter is specifically for this case. From the ag-grid documentation:
valueFormatter用于文本格式化。
cellRenderer适用于您要在单元格中包含HTML标记和
个潜在功能的情况。因此,例如,如果您想将
标点符号放入一个值中,请使用valueFormatter,如果您想将
按钮或HTML链接中使用cellRenderer。可以使用
两者的组合,在这种情况下,valueFormatter
的结果将传递到cellRenderer。
valueFormatter's are for text formatting. cellRenderer's 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 valueFormatter, if you want to put buttons or HTML links use a cellRenderer. It is possible to use a combination of both, in which case the result of the valueFormatter will be passed to the cellRenderer.
这篇关于Ag-Grid:数字格式,例如:123456.78至123,457的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!