最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to format currency in Datatables? - Stack Overflow

programmeradmin6浏览0评论

This is a table which display transactions, implementes using DataTables.

$( document ).ready(function() {    
    var table = $('#tbl_transaksi').DataTable( {
        "ajax": "data_transaksi.php",
        "bPaginate":true,
        "bProcessing": true,
        "pageLength": 10,
        "columns": [
            { mData: 'username' } ,
            { mData: 'fullname' },
            { mData: 'the_date' },
            { mData: 'amount',  render: function ( data, type, row ) {
                return "Rp " + data;
                } 
            }
        ],
        "dom": 'Bfrtip',
        "buttons": [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    }); 

});

It works. Now I want to add an enhancement: format the amount ("jumlah") using Indonesian format, so for example 1000000 will be displayed as "Rp 1.000.000";

A Google search pointed me to renderers. I added the render part into my code, and well it doesn't change the formatting. What is wrong here?

This is a table which display transactions, implementes using DataTables.

$( document ).ready(function() {    
    var table = $('#tbl_transaksi').DataTable( {
        "ajax": "data_transaksi.php",
        "bPaginate":true,
        "bProcessing": true,
        "pageLength": 10,
        "columns": [
            { mData: 'username' } ,
            { mData: 'fullname' },
            { mData: 'the_date' },
            { mData: 'amount',  render: function ( data, type, row ) {
                return "Rp " + data;
                } 
            }
        ],
        "dom": 'Bfrtip',
        "buttons": [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    }); 

});

It works. Now I want to add an enhancement: format the amount ("jumlah") using Indonesian format, so for example 1000000 will be displayed as "Rp 1.000.000";

A Google search pointed me to renderers. I added the render part into my code, and well it doesn't change the formatting. What is wrong here?

Share Improve this question asked Dec 13, 2017 at 9:22 anta40anta40 6,7438 gold badges53 silver badges79 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

use $.fn.dataTable.render.number function,

$( document ).ready(function() {    
    var table = $('#tbl_transaksi').DataTable( {
        "ajax": "data_transaksi.php",
        "bPaginate":true,
        "bProcessing": true,
        "pageLength": 10,
        "columns": [
            { mData: 'username' } ,
            { mData: 'fullname' },
            { mData: 'the_date' },
            { mData: 'amount',  render: $.fn.dataTable.render.number( ',', '.', 3, 'Rp' )
            }
        ],
        "dom": 'Bfrtip',
        "buttons": [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    }); 

});

Hope this thing solve your problem.

$(document).ready(function () {
  var table = $('#tbl_transaksi').DataTable({
    "ajax": "data_transaksi.php",
    "bPaginate": true,
    "bProcessing": true,
    "pageLength": 10,
    "columns": [
      { mData: 'username' },
      { mData: 'fullname' },
      { mData: 'the_date' },
      {
        mData: 'amount', render: function (data, type, row, meta) {
          return meta.settings.fnFormatNumber(row.amount);
        }
      }
    ],
    "dom": 'Bfrtip',
    "buttons": [
      'copy', 'csv', 'excel', 'pdf', 'print'
    ]
  });
});

use it gan :

{
  mData: 'amount',  render: $.fn.dataTable.render.number( '.', ',', 0, 'Rp ' )
 }
//result Rp 175.000

in indonesian rupiah they use dot (.) for currency not comma

发布评论

评论列表(0)

  1. 暂无评论