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

AG-GRID value formatter not working for dynamically generated currency

我正在尝试在我的 AG-GRID table 中使用值格式化程序来显示货币信息。

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

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

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

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

它在控制台中抛出的是:

TypeError: Cannot read property 'defaultCurrency' of undefined

似乎所有 'this' 组件变量在 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 问题。这里有一个不错的read

此外,此 描述了您可以参考的 2 种方式 this