2017-03-27 169 views
0

我目前正在使用一個不通過AJAX填充的數據表,因此我相信我不能使用datasrc選項。打印之前有什麼方法可以格式化我的一列嗎?就像通過一個函數或其他東西。非常感謝您的幫助。 (通過格式化我的意思例如,從「constructionAmount」獲得的價值,並增加了$和一些逗號。)格式化數據表的數據

$("#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 
      }, 
     });  

回答

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,"); 

}

+1

還不能,它說我不能接受我自己的答案,直到2天已經過去了。 – Omaruchan