格式化数据表的数据

时间:2017-03-27 22:34:43

标签: jquery datatables

我目前正在处理一个我不会通过AJAX填充的数据表,所以我相信我不能使用datasrc选项。有没有什么办法可以在打印之前格式化我的一个列?喜欢通过功能或其他东西。非常感谢您的帮助。 (通过格式化我的意思是例如从" constructionAmount&#34获取值;并添加$和一些逗号。)

$("#showDepreciationMontlhytable").DataTable({              
            destroy:true,
            responsive: true,
            autoWidth : true,
            searching: true, 
            data: transaction.transactionDetails,               
            columns:[
                    {"data": "idRealEstate"},
                    {"data": "name"},
                    {"data": "addressState"},
                    {"data": "depreciationDetail.constructionAmount"},
                    {"data": "depreciationDetail.depreciationPercentageYearly"},
                    {"data": "depreciationDetail.monthsUse"},
                    {"data": "depreciationDetail.usefulLifeMonths"},
                    {"data": "depreciationDetail.amount"}
                    ]   ,       
            language: {
                url: datatablesSpanishUrl
            },
        });     

1 个答案:

答案 0 :(得分:0)

找到答案,有“渲染”选项,可让您格式化数据,但需要它。

$("#showDepreciationMontlhytable").DataTable({              
            destroy:true,
            responsive: true,
            autoWidth : true,
            searching: true, 
            data: transaction.transactionDetails,
            columns:[
                    {"data": "idRealEstate"},
                    {"data": "name"},
                    {"data": "addressState"},
                    {"data": "depreciationDetail.constructionAmount",
                    "render" : function( data, type, full ) 
                        {
                            return currencyFormat(data,"$");                          
                        }
                    },
                    {"data": "depreciationDetail.depreciationPercentageYearly"},
                    {"data": "depreciationDetail.monthsUse"},
                    {"data": "depreciationDetail.usefulLifeMonths"},
                    {"data": "depreciationDetail.amount",
                    "render" : function( data, type, full ) 
                        {
                            return currencyFormat(data,"$");                          
                        }
                    }
                    ]   ,       
            language: {
                url: datatablesSpanishUrl
            },
        }); 

function currencyFormat(n, currency) {
return currency + " " + n.toFixed(decimalRounding).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");

}