Reputation: 1319
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?
Upvotes: 13
Views: 39449
Reputation: 109
$('#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;
}
Upvotes: 5
Reputation: 3332
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, '') }
],
Upvotes: 42
Reputation: 6045
As mentioned in my comment you have to do something like this
Inside Datatable initialization :
"aoColumnDefs": [ {
"aTargets": [ 2 ],
"mRender": function (data, type, full) {
var formmatedvalue=data.replace(//regex expression)
return formmatedvalue;
}
}]
Upvotes: 4