How to format currency in Datatables?

纵饮孤独 提交于 2019-12-24 20:06:14

问题


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?


回答1:


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

});


来源:https://stackoverflow.com/questions/47789487/how-to-format-currency-in-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!