My First ExtJS DataGrid Pt 7: Custom Cell Renderers
So, it's been awhile. No, I haven't forgotten you, I've just been busy with a lot of things. One of which has been implementing a new ExtJS DataGrid in a project I'm working on. Sure, there's a lot more going on, but that's becoming a nice front end piece. As previously promised, I want to look at a renderer.
What Is A Renderer?
Hey, not everything you get back from your paging query will be formatted in the way you want it displayed. I'm going to show you a really simple example, dealing with a boolean value. Let's start off by writing a renderer function. Here's the deal, you have a query column with a boolean value (bIsActive), which is returned as either 1 or 0. You want the grid to display Yes or No. You could define the function directly in your ColumnModel declaration:,{
header:'Active',
dataIndex:'bIsActive',
renderer:function(value){
return (value == 1)?'Yes':'No';}
}
...
A renderer function will take at least one argument by default, that being the value of the cell being rendered. You do not explicitly call a renderer, the arguments passed to the renderer are dependent upon the function definition. For something as simple as a Yes/No boolean renderer the value is all that's necessary. So you would define a renderer function, and register it, like this:
return String.format("{0}",(value==1)?'Yes':'No');
}
// And a redefinition of of the renderer in the ColumnModel ...
,{
header:'Active',
dataIndex:'bIsActive',
renderer:renderBoolean
}
...
return String.format("{0} {1}", value, r.data['vcLastName']);
}
// Combine our 'name' columns into one ...
,{
header:'Name',
dataIndex:'vcFirstName',
renderer:renderName
}
...
You'll also want to note the the JavaScript bind variable syntax being used in the String.format() function, {[value]}. The numbers used here are like array position values, starting at zero, for all of the variables passed in the arguments that follow the string you are formatting (the first argument of the function).
So, that's a quick down and dirty on renderers. No download with this particular post, but the code samples above aren't overly difficult to integrate. You can, absolutely, do some much more complex renderer functions, but this should be a good start.

