Twitter: raymondcamden

Address: Lafayette, LA, USA

Custom grid renderers with CFGRID

08-20-2007 47,412 views ColdFusion

I've been playing a bit with CFGRID lately (as have others) and I found that by taking a look at Ext's documentation, there are some very powerful features hiding within the CFGRID tag. ColdFusion provides a function, ColdFusion.Grid.getGridObject, that when used gives you a handler to the underling Ext Grid object. By looking at the documentation, you can see which methods are available with the grid. One in particular I thought was pretty neat was the ability to add custom column renderers. What is that?

Well imagine you have data being fed into the grid that you do not have control over. For example - perhaps you have price information that isn't formatted nicely. Turns out Ext has a set of built in formatters that you can apply to grid columns, one of them being a money formatter. What if you have some other logic? Maybe you want to flag a price that is lower than 10 dollars? Again, using the Ext API, you can write your own formatter just for this purpose.

I've only just begun to scratch the surface of Ext, but here is a quick and dirty example to give you an idea of what is possible.

First lets create a simple grid:

view plain print about
1<cfset data = queryNew("price,product")>
2<cfloop from=1 to=10 index="x">
3    <cfset total = randRange(20,100) & "." & randRange(1,99)>
4    <cfset product = "Product #X#">
5    <cfset queryAddRow(data)>
6    <cfset querySetCell(data, "price", total+0, x)>
7    <cfset querySetCell(data, "product", product, x)>
10<cfform name="test">
11<cfgrid autowidth="true" name="data" format="html" query="data" width="600">
12 <cfgridcolumn name="price" header="Price">
13 <cfgridcolumn name="product" header="Product">

I'm using a hard coded query with products and prices. This is then fed directly into the grid. Note that I'm not using Ajax here.

Now let's look at how we can add a custom column renderer. The first thing we need to do is to set up the page to call a function when the grid is done loading. We do this with ajaxOnLoad:

view plain print about
1<cfset ajaxOnLoad("testgrid")>

Because I'm not "properly" using Ajax on my page, I also added an import:

view plain print about

I've mentioned this hack before, and credit goes to Todd Sharp for discovering. Now let's look at the JavaScript:

view plain print about
1myf = function(data,cellmd,record,row,col,store) {
2    if(data == "Product 4") return "<b>" + data + "</b>";
3    else return data;
5testgrid = function() {
6    mygrid = ColdFusion.Grid.getGridObject('data');
7    cm = mygrid.getColumnModel();
8    cm.setRenderer(0, Ext.util.Format.usMoney);
9    cm.setRenderer(1,myf);
10    mygrid.reconfigure(mygrid.getDataSource(),cm);

Skip the first function and focus in on testgrid. Testgrid is a horrible name but I was just playing around with the code so forgive me. I grab the grid using the ColdFusion.Grid.getGridObject API mentioned before. Everything after this is based on my reading of the Ext docs. I grab the columns using the getColumnModel function. I apply a renderer to columns 0 and 1 (you will never convince me that 0 based indexes make sense). The first renderer is a built one named usMoney. The second one is a custom function named "myf". usMoney will do what you imagine - format money. The second was hard coded to look for Product 4, and when found, bold it. I then use reconfigure to apply the column model back to my grid.

You can see an example of this here. Full source code is below.

view plain print about
7myf = function(data,cellmd,record,row,col,store) {
8    if(data == "Product 4") return "<b>" + data + "</b>";
9    else return data;
11testgrid = function() {
12    mygrid = ColdFusion.Grid.getGridObject('data');
13    cm = mygrid.getColumnModel();
14    cm.setRenderer(0, Ext.util.Format.usMoney);
15    cm.setRenderer(1,myf);
16    mygrid.reconfigure(mygrid.getDataSource(),cm);
23<cfset data = queryNew("price,product")>
24<cfloop from=1 to=10 index="x">
25    <cfset total = randRange(20,100) & "." & randRange(1,99)>
26    <cfset product = "Product #X#">
27    <cfset queryAddRow(data)>
28    <cfset querySetCell(data, "price", total+0, x)>
29    <cfset querySetCell(data, "product", product, x)>
32<cfform name="test">
33<cfgrid autowidth="true" name="data" format="html" query="data" width="600">
34 <cfgridcolumn name="price" header="Price">
35 <cfgridcolumn name="product" header="Product">
39<cfset ajaxOnLoad("testgrid")>