【问题标题】:Format a bootstrap table with dollar sign and thousand separator [closed]使用美元符号和千位分隔符格式化引导表 [关闭]
【发布时间】:2016-03-22 18:58:12
【问题描述】:

我正在尝试格式化我的引导表,以便我可以读取 $12,000,000 而不是 12000000 + 它的页脚(汇总列) 我知道这可以通过插入 javascript 函数 +CSS 来完成。 感谢您的帮助。

这是我的表的代码。

<table id="exampleTableFromData" data-sort-order="desc" data-mobile-responsive="true" data-search="true" data-show-footer="true" data-footer-style="footerStyle">
    <thead>
        <tr>
            <th data-field="Customers" data-sortable="true" data-align="left">Customer</th>

            <th data-field="RM" data-sortable="true" data-align="right" data-footer-formatter="totalFormatter">Sales</th>

            <th data-field="BO" data-sortable="true" data-align="right" data-footer-formatter="totalFormatter">Discount</th>

            <th data-field="GGR" data-sortable="true" data-align="right" data-footer-formatter="totalFormatter">Net</th>
        </tr>
    </thead>
</table>

【问题讨论】:

  • 你应该添加更多不相关的语言标签...
  • 完成了——现在好点了吗?
  • 假设这是PHP,试试number_format
  • 您使用哪种服务器端语言?
  • 服务端返回一个json

标签: javascript twitter-bootstrap currency-formatting


【解决方案1】:

好的,找到了使用此脚本为页脚设置正确格式的方法

function totalFormatter(data) {

  var total = 0;

  if (data.length > 0) {

    var field = this.field;

    total = data.reduce(function(sum, row) {
      return sum + (+row[field]);
    }, 0);
    var num = '$' + total.toFixed(0).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    return num;
  }

  return '';
};

【讨论】:

    【解决方案2】:

    如果这是在 PHP 中,请尝试以下操作:

    $num = 12000000;
    echo '$' . number_format($num);    // $12,000,000
    echo '$' . number_format($num, 2); // $12,000,000.00
    

    【讨论】:

    • 不在由 jquery 提供支持的表中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多