In our previous examples we had been using plain Strings in ExtJs grid.
Have a quick look at them:
- On-Demand Paging using ExtJS Grid with Client-Centric ASP.NET AJAX (WebMethods)
- Adding server side sorting to ExtJS Grid using WebMethods in ASP.NET
Today, we are going to deal with other Data types like DateTime, Boolean, Currency et cetera. For this purpose, ExtJs ColumnModel provides and excellent config option renderer
Lets start with builtin format options of ExtJs.
// create the grid
var grid = new Ext.grid.EditorGridPanel({
el:'grid-employees',
width:650,
height:300,
title:'Sample Employees',
store: store,
stripeRows: true,
loadMask: true,
enableHdMenu: false,
// grid columns
columns:[{
id: 'EmployeeID',
dataIndex: 'EmployeeID',
hidden:true
},{
header: "Login ID",
dataIndex: 'LoginID',
width: 150,
sortable:true
},{
header: "Title",
dataIndex: 'Title',
width: 200,
sortable:true
},{
header: "Birth Date",
dataIndex: 'BirthDate',
width: 100,
sortable:true
},{
header: "Gender",
dataIndex: 'Gender',
width: 70,
sortable:true
},{
header: "Salary",
dataIndex: 'Salary',
width: 70,
renderer: 'usMoney',
sortable:true
},{
header: "Salaried",
dataIndex: 'SalariedFlag',
width: 50,
sortable:true
}],
// put paging bar on the bottom
bbar: pagingBar
});
Notice that for the dataIndex Salary, we have renderer option equal to usMony. This will render all values in this column as usMoney.
Now lets play with some custom renderers. I have written two custom rederers. One is for DateTime object of .NET and the other is for Boolean object.
// Our own date renderer function
function formatDate(value){
return value ? value.dateFormat('F d, Y') : '';
};
// Our own boolean renderer
function formatTrueFalse(value) {
return value ? '✓': '☓';
}
These functions are quite simple. They just return the formatted strings to be used in ColumnModel. Now we will use these renderers in our ColumnModel.
// create the grid
var grid = new Ext.grid.EditorGridPanel({
el:'grid-employees',
width:650,
height:300,
title:'Sample Employees',
store: store,
stripeRows: true,
loadMask: true,
enableHdMenu: false,
// grid columns
columns:[{
id: 'EmployeeID',
dataIndex: 'EmployeeID',
hidden:true
},{
header: "Login ID",
dataIndex: 'LoginID',
width: 150,
sortable:true
},{
header: "Title",
dataIndex: 'Title',
width: 200,
sortable:true
},{
header: "Birth Date",
dataIndex: 'BirthDate',
width: 100,
renderer: formatDate,
sortable:true
},{
header: "Gender",
dataIndex: 'Gender',
width: 70,
sortable:true
},{
header: "Salary",
dataIndex: 'Salary',
width: 70,
renderer: 'usMoney',
sortable:true
},{
header: "Salaried",
dataIndex: 'SalariedFlag',
width: 50,
renderer: formatTrueFalse,
sortable:true
}],
// put paging bar on the bottom
bbar: pagingBar
});
And that’s all
Now you will have a beautifully rendered grid.

You can also add images in grid in this way. Try replacing Gender in this example with appropriate icons. I leave this as an exercise for you.





