Home Contact Us

n42 Designs

ColdFusion and Flex Web Development

n42 Designs

n42 Designs specializes in custom web development using the Adobe web development platform, ColdFusion and Flex. For web sites, web applications, large and small, let us know how we can help your business succeed!

Search

n42 Designs

Recent Comments

Subscribe

RSS

 

Filtering a CFGrid

So you want to filter a CFGrid huh? ColdFusion makes this easy!

First you need a CFC method that will return the query used to populate the grid. This method should take the cfgrid parameters and the value you want to filter on.

data.cfc:

<cfcomponent>
<cffunction name="getdata" access="remote" output="false"
returntype="Any">

<cfargument name="page"
required="false" default="1" type="Numeric" />

<cfargument name="pagesize" required="false" default="10" type="Numeric"
/>

<cfargument name="sortcolumn" required="false"
default="" type="string" />

<cfargument name="sortdir"
required="false" default="ASC" type="string" />

<cfargument name="filter" required="false" default="" type="String"
/>
>

<cfset q =
queryNew('id,name','integer,varchar') /
>

<cfset queryaddrow(q,1) />
<cfset querysetcell(q,'id',1)
/
>

<cfset querysetcell(q,'name','sean') />
<cfset queryaddrow(q,1) />
<cfset
querysetcell(q,'id',2) /
>

<cfset
querysetcell(q,'name','phillip') /
>

<cfset queryaddrow(q,1) />
<cfset querysetcell(q,'id',3)
/
>

<cfset querysetcell(q,'name','steve') />
<cfquery name="q" dbtype="query">
select * from q
<cfif
len(trim(arguments.filter))>

where name like <cfqueryparam cfsqltype="cf_sql_varchar"
value="%#arguments.filter#%" />

</cfif>
<cfif
len(trim(arguments.sortcolumn)) or len(trim(arguments.sortdir))>

order by #arguments.sortcolumn# #arguments.sortdir#
</cfif>
</cfquery>
<cfreturn
queryConvertForGrid(q,arguments.page,arguments.pageSize) />

</cffunction>
</cfcomponent>

You can see here that I am just hard coding the query in, you would replace this with your cfquery call to your database or a call to your service layer, etc. Once you have your query results, you want to use the queryConvertForGrid method to format the result to a value that CFGrid can work with.

You then need a cfgrid! The trick here is how to get the grid to refresh as the user types a value in the filter box. ColdFusion Ajax UI tools have bind expressions that let us bind one control to another, and to react to events.

index.cfm:

<cfform>
<p><label for="filter"><cfinput type="text" name="filter"
id="filter" />
</p>
<cfgrid format="html"
bind="cfc:data.getData({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection},{filter@keyup})"
name="grd" bindonload="true">

<cfgridcolumn name="id" />
<cfgridcolumn name="name" />
</cfgrid>
</cfform>

You can see my bind expression in the cf grid. It will call the getData method of my data.cfc and pass in the value from the "filter" input box whenever the key is released. ColdFusion provides other events you can react to as well, check the docs for details.

So now if you run index.cfm you will see a simple form with a text input and a grid. the grid will load when the page is run (bindonload="true") and as you type in the text box, the results are filtered.

EDIT: removed the example link as I have moved this blog to Railo

Webservices Change in CF 8.0.1

It seems with the change to 8.0.1 there is a difference in the way CF generates the stubs for a CFC called as a web service. I had a CFC called webservices.cfc and on 8.0 it was working fine calling the methods, but with the 8.0.1 it started throwing a "duplicate file name" error. This seems to be because CF creates its own stub file Webservices.java and also a [cfcname].java file. CF couldn't create both so it threw an error. The solution was to rename my CFC. The stubs can be found in C:\ColdFusion8\stubs\ on windows, and if I remember correctly /Applications/ColdFusion8/stubs/ on Mac OS X.

Issue w/ Eclipse Resource Bundle Editor Plugin and Leopard

There seems to be an issue with the Resource Bundle Editor Plugin for Eclipse. (http://sourceforge.net/projects/eclipse-rbe/)

I installed version 0.7.7 into a fresh install of the Eclipse 3.3 SDK and was unable to open a resource file. I would get the following error:

Can't start the AWT because Java was started on
the first thread. Make sure StartOnFirstThread is not specified in your
application's Info.plist or on the command line

To fix it I edited the eclipse.ini file (/Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini by default) to add the following line:

-Djava.awt.headless=true

Once I did that it was fine and I was able to edit the resource bundles.

Hope this helps someone searching for the issue!