2016-10-06 67 views
0

將此用作example如何控制單元格中值的格式?格式化數據表中的單元值

例如,我將如何格式化Extn.列以讀取5,407或54.07?

Name Position Office Extn. Start date Salary 
Airi Satou Accountant Tokyo 5407 $2008/11/28 $162,700 

我一直在尋找herehere,但我不能完全解決它。任何人都可以建議?

我已經試過這樣的事情,但我有沒有成功:

$(document).ready(function() { 
    $('#example').DataTable({ 
     data: dataSet, 
     columns: [ 
      { title: "Name" }, 
      { title: "Position" }, 
      { title: "Office" }, 
      { title: "Extn." }, 
      { title: "Start date" }, 
      { title: "Salary" } 
     ], 
     "aoColumnDefs": [ { 
        "aTargets": [ 4 ], 
         "mRender": function (data, type, full) { 
           //var formmatedvalue=data.replace("TEST") 
          //return formmatedvalue; 
        return '$'+ data; 
          } 
      }] 
    }); 
}); 

回答

1

使用columns.renderoption

或者與built-in幫手,讓千個分隔符(5,407):

{ 
    title: "Extn.", 
    render: $.fn.dataTable.render.number(',', '.', 0, '') 
}, 

JSFiddle example

或者自己使用自定義功能(54.07)做到這一點:

{ 
    title: "Extn.", render: function (data, type, row) { 
     return data/100; 
    } 

}, 

JSFiddle example

+0

非常感謝!很好的答案 – HattrickNZ

相關問題