Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery datatables format numbers

I use the latest Datatables plugin 1.10 version.

I have 3 columns (0 , 1, 2). Columns 1 and 2 contain numbers which should be formatted like:

1000 -> 1.000
10000 -> 10.000

I searched the documentation and I found these relevant functions:

https://datatables.net/reference/option/formatNumber

https://datatables.net/reference/option/language.thousands

Are the columns that need to be formatted detected automatically?

What is the correct usage of the above functions?

like image 497
gosom Avatar asked Dec 09 '14 13:12

gosom


2 Answers

There is actually an even easier way to do this, also found on the datatables documentation:

        "columns": [
            { "data": "ReceiptQuantity", render: $.fn.dataTable.render.number(',', '.', 2, '') },
            { "data": "ReceiptPrice", render: $.fn.dataTable.render.number(',', '.', 2, '') },
            { "data": "LineTotal", render: $.fn.dataTable.render.number(',', '.', 2, '') }
        ],
like image 189
JustLearning Avatar answered Sep 24 '22 17:09

JustLearning


$('#table-dg').dataTable({
    "columns": columnNames,
    "columnDefs": [
        {
            "render": function (data, type, row) {
                 return commaSeparateNumber(data);
            },
            "targets": [1,2]
        },
    ]
});

function commaSeparateNumber(val) {
    while (/(\d+)(\d{3})/.test(val.toString())) {
        val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
    }

    return val;
}
like image 29
RAS Avatar answered Sep 24 '22 17:09

RAS