【问题标题】:AG-GRID value formatter not working for dynamically generated currencyAG-GRID 值格式化程序不适用于动态生成的货币
【发布时间】:2020-08-25 04:33:11
【问题描述】:

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

当我在格式化程序中有一个硬编码的值时,这非常有效,在这种情况下是“欧元”的 unicode

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

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

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

它在控制台中抛出的是:

TypeError: Cannot read property 'defaultCurrency' of undefined

似乎所有“this”组件变量在currencyFormatter 中都不可用。有没有办法让这个工作?

【问题讨论】:

    标签: ag-grid ag-grid-angular


    【解决方案1】:

    为了访问您的组件变量,您必须将您的组件上下文 - 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

    此外,此answer 描述了您可以引用this 的两种方式。

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多