AG-GRID值格式化程序不适用于动态生成的货币



我正试图在AG-GRID表中使用一个值格式化程序来显示货币信息。

当我在格式化程序中有一个硬编码的值时,这非常有效,在本例中是"Euros"的unicode

currencyFormatter(params) {
return 'u20ac' + params.value;
}

然而,我事先不知道我需要用什么货币来格式化数据,因为它是动态生成的。如果我尝试使用组件中可用的值(如下所示(,它不喜欢!

currencyFormatter(params) {
return this.currencyUnicode + params.value;
}

它在控制台中抛出的是:

TypeError: Cannot read property 'defaultCurrency' of undefined

currencyFormatter中似乎没有所有"this"组件变量。有没有办法做到这一点?

为了访问组件变量,您必须将组件上下文绑定到valueFormatter

...
name : 'Currency',
field : 'currency',
valueFormatter: this.currencyFormatter.bind(this) //bind your component's context here
...
currencyFormatter(params) {
return this.currencyUnicode + params.value;
}

这是一个常见的javascript问题。这是一个很好的阅读

此外,这个答案描述了引用this的两种方式。

最新更新