首页 > 解决方案 > 如何格式化烧瓶动态表中的值

问题描述

我从 API 中提取数据,在 ETL 过程中,我将它们转换为没有任何格式的纯数字,以便可以执行公式。

但是,我希望数据在前端表中调用后显示转换为货币结果。

这是动态表渲染:

<!-- Equity Holdings Performance Table -->
        <table class="table table-hover table-striped table-sm">
          <tr>
            <th>ID</th>
            <th>Trade Date</th>
            <th>Price Gain</th>
          </tr>
          {% for row in all_equity_data_html %}
          <tr>
            <td>{{row.equity_id}}</td>
            <td>{{row.equity_date_purchase}}</td>      
            <td> &#x24; {{row.equity_price_gain}}</td>
          </tr>
          {% endfor %}
        </table>

这是适用于一个字段但不适用于动态表的转换脚本:

<script>const formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2
})
  formatter.format(row.equity_price_gain)
</script>

<script>
  $(document).ready( function() {
  $("td.equity_price_gain").each(function() { $(this).html(parseFloat($(this).text()).toLocaleString('en-US', {style: 'currency', currency: 'USD'})); })
  })
</script>

如何修复以下脚本?(我确实尝试了两种变体,看看我是否可以通过另一种方式获得它)

非格式化表格

标签: javascript

解决方案


推荐阅读