Jquery 数据表格式数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27380390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery datatables format numbers
提问by gosom
I use the latest Datatables plugin 1.10 version.
我使用最新的 Datatables 插件 1.10 版本。
I have 3 columns (0 , 1, 2). Columns 1 and 2 contain numbers which should be formatted like:
我有 3 列(0、1、2)。第 1 列和第 2 列包含数字,其格式应为:
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/formatNumber
https://datatables.net/reference/option/language.thousands
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?
以上函数的正确用法是什么?
采纳答案by super cool
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;
}
}]
回答by JustLearning
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, '') }
],
回答by RAS
$('#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})/, '' + ',' + '');
}
return val;
}