问题描述
我有一个包含4列包含货币的数据表.目前,我将它们视为普通列,并在每个值后手动添加"$".现在,我需要格式化该列以使其也带有逗号.是否有任何插件可以做到这一点?我也想删除手动添加的"$"值.我检查了几个站点,但我真的不明白它们是如何工作的.
I have a datatable with 4 columns that hold currency. Currently I'm treating them as normal columns and manually appending '$' to each value. Now I need to format the column to have commas as well. Is there any plug-in to do this? I also want to remove the manual addition of '$' value. I checked few sites, but I really didn't understand how they work.
推荐答案
[更新答案以使用DataTables 1.9+并尊重rynop的更好答案.原始答案保留在水平线以下,但是既过时又效率不高.]
由于实际上是您要修改的数据,而不是整个单元格,因此应在列定义中使用"render"属性.要生成干净的代码,可以将实际的修改方法存储在其他位置,然后调用它:
Since it is really the data that you want to modify, not the entire cell, you should be using the "render" property inside of the columns definition. To produce clean code, you could store the actual modification method elsewhere and just call over to it:
var myTable = $('#mytable').DataTable({
...
"columns": [
{
"data" : "key_of_column",
"render" : function( data, type, full ) {
// you could prepend a dollar sign before returning, or do it
// in the formatNumber method itself
return formatNumber(data);
}
}
]
...
});
// elsewhere... probably more likely in a utility namespace or module
function formatNumber(n) {
return n.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
formatNumber
使用此已接受答案中的正则表达式:
formatNumber
uses the regex from this accepted answer:
[原始答案]
我自己不会在每个单元格中增加美元价值,这一决定具有降低所需操作复杂性的副作用. ;-)在任何电子表格或货币值表中,您只需要将货币符号放在标题或第一行中即可.将其放在标题中将使您的生活更加轻松,将其放在第一行中实际上会增加问题的复杂性.
I wouldn't add dollar values to each cell myself, a decision that has the side-effect of reducing the complexity of what you need to do. ;-) In any spreadsheet or table of currency values, you only need to put the currency symbol in the header or the first row. Putting it in the header will make your life easier, putting it in the first row will actually add complexity to your problem.
因此,回到DataTables本身,您可以通过多种方法为这只猫换皮,但是这里有两种:
So, back to DataTables itself, you have multiple ways to skin this cat but here are two:
-
不使用逗号(即默认的DataTables行为)呈现整个表,而是使用sClass参数将类添加到那些特定的单元格中.然后使用fnDrawCallback触发上面链接中所述的创建逗号的功能.
Render the whole table without the commas (ie. default DataTables behaviour) but adding a class to those particular cells with the sClass parameter. Then use fnDrawCallback to fire the comma-creating function as described in the above link.
仅使用上述链接中的正则表达式在数据输入时修改单元格.
Use only the regex from the above link to modify the cell as data comes in.
类似这样的东西(其中3是您要修改的零索引列):
Something like this (where 3 is the zero-index column that you're modifying):
"aoColumnDefs": [ {
"aTargets": [3],
"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
var $currencyCell = $(nTd);
var commaValue = $currencyCell.text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
$currencyCell.text(commaValue);
}
}]
(aoColumnDefs
是传递给dataTable()
的初始化对象的一部分);
(aoColumnDefs
is part of your initialization object passed to dataTable()
);
如果绝对必须添加美元符号,则只需将其放在text
函数之前的同一函数中即可.
If you absolutely MUST add the dollar sign, you would just prepend it in the same function before the text
function.
这篇关于数据表列中的逗号和$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!