Filtering CFGrid with CFSelect

A reader asked me yesterday about filtering a CFGrid using a CFSelect. I am quite happy to say that it is dead easy to do using the new CF8 tags. You can also bind your CFGrid to more than one control you just need to handle it with your cffunction.

Take the following grid for example. The grid is bound to a tree control as well as another control. The tree control sends the directory to the geFiles function to return a list of files.

<cfgrid name="fileGrid" format="html" sort="true"
   bind="cfc:#request.cfcpath#fileExplorer.fileExplorer.getfiles({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection},{mytree:directories.node},{extensions})">


   <cfgridcolumn name="name" header="File Name" display="true"/>
   <cfgridcolumn name="size" header="Size" display="true"/>
   <cfgridcolumn name="datelastModified" header="Last Modified" width="150"/>
   <cfgridcolumn name="directory" header="" display="true"/>
   </cfgrid>

The extensions control is cfselect that returns a list of file extensions that the results can be filtered with. The default item in the select control is an empty string which will indicate to the CFC not to filter the items.

<cfselect name="extensions" bind="cfc:#request.cfcpath#fileExplorer.fileExplorer.getfileExtensions()" bindonload="true">

Then in the CFC need to make sure that you have the required grid arguments as well as the optional arguments.

<cfargument name="page" type="numeric" required="yes"/>
   <cfargument name="pageSize" type="numeric" required="yes"/>
   <cfargument name="gridSortColumn" type="string" required="yes"/>
   <cfargument name="gridSortDirection" type="string" required="yes"/>
   <cfargument name="path" type="string" required="false" default="/"/>
   <cfargument name="filter" type="string" required="false" default="">

And there you have it, a CFGrid that is bound to two different controls, a tree control and a cfselect.

Happy Coding...

Comments