Search code examples
javascripttwitter-bootstrapcurrency-formatting

Format a bootstrap table with dollar sign and thousand separator


I am trying to format my bootstrap table, so that I can read $12,000,000 instead of 12000000 + its footer (suming up columns) I understand this can be donne inserting a javascript function +CSS. Thanks for your help.

Here is the code of my table.

<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>

Solution

  • ok found the way to have the right format for the footer with this script

    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 '';
    };