Sunday, August 3, 2008

CFGRID and CFC Binding How To

I avoided using a CFC binding initially because I didn't grok how and, before I knew that my route made pagination impossible, a query in the HTML CFGRID seemed so much easier. But here is how the binding part is done.

In the CFC file you're going to have the following:

1. A function name with arguments
2. Your query
3. Not always but in my case, a query return for the grid.

The function is going to have remote access and return a structure. Here is my code for it:


<cffunction name="comments" access="remote" returntype="struct">
<cfargument name="page" required="true">
<cfargument name="pageSize" required="true">
<cfargument name="gridsortcolumn" required="true">
<cfargument name="gridsortdirection" required="true">


Page means, "Do you want this to do paging, where things are broken into separate pages?" PageSize means, "How big (how many records) should each page be?" The sorts are fairly obvious I think...

If you have the above it means you will have to have the page and pageSize defined in your CFGRID tag in your CFM page, and you can control it from there. I did not define my grid sorts and it didn't break.

Next you have your query. Whatever query you were using to output records prior to CFGRID will probably work fine, but I ended up having to hack mine to do all kinds of stuff that CFGRID made a royal pain in the butt, so I fixed it through SQL instead of CFML. Will cover that in another post.

Then you have your query "return". You find this in documentation under functions, queryconvertforgrid function.

In my case it has three values:
1 - the name of my query
2 - page
3 - pagesize

And I mean those strings (page and pagesize) not true/numeric. Here's my code, where my query is called selectAll:


<cfreturn queryconvertforgrid(selectAll,page,pagesize)/>


Now to put the CFC together you'll want these three things in order. I have a file called getcomments.cfc and here is the code, with the query revised for public use of course. Note that the CFC did not find my datasource which is a variable set in my Application.cfm file, so I had to manually CFSET that value before running the query.


<cffunction name="comments" access="remote" returntype="struct">
<cfargument name="page" required="true">
<cfargument name="pageSize" required="true">
<cfargument name="gridsortcolumn" required="true">
<cfargument name="gridsortdirection" required="true">

<cfquery datasource="myds" name="selectAll" maxrows="1000">
My query goes here
</cfquery>

<cfreturn queryconvertforgrid(selectAll,page,pagesize)/>
</cffunction>


Then in my CFM file, my HTML format CFGRID used this code to call it:


<cfform name="myform" id="myformid">
<cfgrid
name = "myGrid"
format="html"
bindOnLoad="yes"
bind="cfc:getcomments.comments({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})"
pageSize="10"
preservePageOnSort="true"
selectMode="single"
selectOnLoad="no"
font="Verdana"
fontsize="11"
label="True"
stripeRows="true"
stripeRowColor="##D9CCFF"
selectColor="##994FE6"
>
<cfgridcolumn name="id" display="no" >
<cfgridcolumn name="DateEntered" display="yes" header="Date" mask="EEE DD-MMM-YY H:NN A">
<cfgridcolumn name="Alias" italic="yes" display="yes" header="From">
<cfgridcolumn name="See" width="35" display="yes" >
<cfgridcolumn name="theComment" fontsize="1.2em" textColor="##661A99" width="700" display="yes" header="Comment">
</cfgrid>
</cfform>


Notice that there is some styling in one of the columns and that works, and date formatting in the date column which did not work at all so I did something different, which I'll cover in another post. The 'see' value there is a big string from a special SQL call that gave me an HREF link, and an image, and appended the record ID, because stupid CFGRID wouldn't let me get rid of underlining every damn thing if I used the HREF attribute.

5 comments:

Anonymous said...

I am new to CFGRID and CFC so your blog was a huge help! I am having trouble adding a hyperlink to the rows... I will check your others entries.

Thank you very much!

Eric

Anonymous said...

How do you pass in a variable to the binding cfc? I've got a page where I want to filter what shows in the grid to be specific to the user, based on a session variable. How do I pass in the value of that session variable on the call to the CFC so the query in the CFC can use it?

PJ said...

"How do you pass in a variable to the binding cfc?" Will respond this evening when I get off work. - PJ

Anonymous said...

The only way I have been able to do it is using form elements, so, i do it like so (gotta add back in the > and <'s):

input type="hidden" name="hSFV" id="hSFV" value="#attributes.ScoreFilterValue#" /

cfgrid name="myGrid" format="HTML" height="450" width="400" striperows="Yes" striperowcolor="gainsboro" maxrows="50" selectmode="row" bind="cfc:query.cfc.ActionPlanSummaryCFC.GetActionPlanSummaryData({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection},{hSFV})"

Anonymous said...

in the cfgrid tag:

href="javascript:clickActivation(ColdFusion.getElementValue('FirstGrid','FirstGridForm','DBFIELD_ID'));"