To format a data table column, you set the Formatter property, which is a function that contains two parameters:
Value: Values for the current column of the corresponding field
Record: Recording data for current row
Copy Code code as follows:
$ (' #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 ' <span style= ' color:red; > (' +val+ ') </span> ';
} 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 numeric formatting, you can use JavaScript-provided methods for formatting numbers
Copy Code code as follows:
var num=2.4445;
var number=val.tofixed (2);//formatting, reserving two decimal places
alert (number);
Output:
2.44