在创建dataTable时格式化货币 [英] Format currencies when dataTable is created

查看:207
本文介绍了在创建dataTable时格式化货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据表({
bJQueryUI:true,
aaData:jsonList,
bPaginate:true,
aoColumns:[
{
mDataProp:null,
sClass:control center,
sDefaultContent :'< img src =http://i.imgur.com/SD7Dz.png>'
},
{mDataProp:ente},
{ mDataProp:cup},
{mDataProp:decreto},
{mDataProp:data},
{mDataProp:importoImpegno} ,//这是一个货币
{mDataProp:finanziato},//这是一个货币
{mDataProp:importoPagato},//这是一个货币
{mDataProp:importoInPagamento} //这是一个货币
],
aoColumnDefs:[
{sClass:货币,aTargets:[5 ,6,7,8]}
],
oLanguage:{
sInfo:_TOTAL_ entries
},
aaSort ing:[[1,'asc']]
});

如您所见,我添加 .currency 只能到一个货币的列。
我需要格式化这种货币(例如3235到3.235,00),我已经有了这个功能。

 函数currencyFormatIT(num){

if(num!= null&& num!=){
num = parseFloat(num)
num = num
.toFixed(2)
.replace(。,,)
.replace(/(\d)(?= d {3})+(?! \d))/ g,$ 1);
}
else {
num =;
}

return num;
}

我尝试使用这种方法:


每个(function(){
$(this).html(currencyFormatIT(());

  $(table.myTable> tbody td.currency $(this).html())); 
$(this).css(text-align,right);
});

但是,它只适用于第一页中显示的行的数据:





从第二页到第:





如果我添加这个代码:

  $(document).on(click,td.currency,function(){
alert this).html());
});

适用于所有页面的所有td。



1)为什么?
2)如果我想调用currencyFormatIT()函数,如回调函数(也许在我创建dataTable的代码 .dataTable({.. 我做吗?

解决方案

您可以使用 fnDrawCallback p>

  var oTable = $('#table')。dataTable({

fnDrawCallback:function oSettings){
//格式货币
}

}

您可以查看文档以获取有关可能的回调函数的更多详细信息。


var oTable = $('#table').dataTable({
    "bJQueryUI": true,
    "aaData": jsonList,
    "bPaginate": true,
    "aoColumns": [
        {
           "mDataProp": null,
           "sClass": "control center",
           "sDefaultContent": '<img src="http://i.imgur.com/SD7Dz.png">'
        },
        { "mDataProp": "ente" }, 
        { "mDataProp": "cup" }, 
        { "mDataProp": "decreto" }, 
        { "mDataProp": "data" }, 
        { "mDataProp": "importoImpegno" }, //this is a currency
        { "mDataProp": "finanziato" }, //this is a currency
        { "mDataProp": "importoPagato" }, //this is a currency
        { "mDataProp": "importoInPagamento" } //this is a currency  
    ],
    "aoColumnDefs": [
     { "sClass": "currency", "aTargets": [ 5, 6, 7, 8 ]}
     ],
    "oLanguage": {
        "sInfo": "_TOTAL_ entries"
    },
    "aaSorting": [[1, 'asc']]
});

As you can see, I add .currency class only to the column where is a currency. I need to format this currencies (3235 to 3.235,00 for example) and I have already the function to do that.

function currencyFormatIT(num) {

    if(num != null && num != "") {
        num = parseFloat(num);
        num = num
        .toFixed(2)
        .replace(".", ",")
        .replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
    }
    else {
        num = "";
    }

    return num;
}

I tried to use this method so:

$("table.myTable > tbody td.currency").each(function(){
    $(this).html(currencyFormatIT($(this).html()));
    $(this).css("text-align", "right");
});

But, it's work only with the row that appears in the first page of datatable:

From the second page to onwards not:

If I add this code:

$(document).on("click", "td.currency", function(){
    alert($(this).html());
});

works for all td of all page!!

1) Why? 2) If I want to call the currencyFormatIT() function like callback function (maybe in the code where I create the dataTable .dataTable({.. how could I do?

解决方案

You can use the fnDrawCallback function.

var oTable = $('#table').dataTable({

   "fnDrawCallback": function (oSettings) {
      // Format Currency here
    }

}

You can check the documentation for more details about possible callback functions.

这篇关于在创建dataTable时格式化货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆