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:2 ,{
3 header:'Active',
4 dataIndex:'bIsActive',
5 renderer:function(value){
6 return (value == 1)?'Yes':'No';}
7 }
8 ...
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:
2 return String.format("{0}",(value==1)?'Yes':'No');
3 }
4
5 // And a redefinition of of the renderer in the ColumnModel
6 ...
7 ,{
8 header:'Active',
9 dataIndex:'bIsActive',
10 renderer:renderBoolean
11 }
12 ...
2 return String.format("{0} {1}", value, r.data['vcLastName']);
3 }
4 // Combine our 'name' columns into one
5 ...
6 ,{
7 header:'Name',
8 dataIndex:'vcFirstName',
9 renderer:renderName
10 }
11 ...
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.


#1 by Flyer on 12/28/07 - 1:50 PM
#2 by Nick on 8/2/08 - 4:02 AM
I know this is not a forum, but i'm working on a problem now with extjs, and being a noobiwe its hard to write the good code for working, and i'm wondering if your experince confrunt with some similar problem.
So i wanna do a master/details relationship between 2 grids in extjs. Thats consist in:
- i have 2 grids, first is a master and second is the slave grid.
- when i select a row in the first grid the second one, the slave grid will show me the results depending on the id_row selected in the first grid.
This is the problem, and i'm wondering if you had some experience with that and if you can post some example.
It wiil be greatfull.
Thx in advance!
#3 by JustAFriend on 8/7/08 - 6:56 PM
hth
#4 by Jenny on 9/18/08 - 11:41 AM
#5 by Amit Jhaiya on 2/20/09 - 3:52 AM
I am amit, I just want help from u tht is is possible to provide a link through which if we point to a particulat element in a grid then we can provide another window if user want.
#6 by Steve 'Cutter' Blades on 10/16/10 - 10:02 AM
Sorry, I just accidentally killed your comment:
"Great post...I've been looking for more information on how to actually create my own renderer for a column. Sencha's site really wasn't very helpful in that regard."
The Sencha site actually has some great stuff for this, if you look in the right place. The best thing you can do is look at the example demos and comb their source code (they usually link it). This will give you a lot of insight into renderers, and show you hard examples of things that may not be overly clear in the API docs.