To format a data table column, you need to set the formatter attribute, which is a function that contains two parameters:
value: the value of the current column of the corresponding field
record: the record data of the current row
$('#tt').datagrid({
title:'Formatting Columns',
width:550,
height:250,
url:'datagrid_data.json',
columns:[[
{field:'itemid', title:'Item ID',width:80},
{field:'productid',title:'Product ID',width:80},
{field:'listprice',title:'List Price' ,width:80,align:'right',
formatter:function(val,rec){
if (val < 20){
return '(' val ')';
} else {
return val;
}
}
},
{field:'unitcost',title :'Unit Cost',width:80,align:'right'},
{field:'attr1',title:'Attribute',width:100},
{field:'status',title: 'Status',width:60}
]]
});
For number formatting, you can use the number formatting method provided by javascript
var num=2.4445;
var number=val.toFixed(2) ;//Format, keep two decimal places
alert(number);
Output:
2.44