Shaun Mccran

My digital playground

20
A
U
G
2009

Connecting Select form fields based on data selections Pt 3

I've been exploring various methods of using JQuery to populate Select form fields. In the previous article I used an intermediary file to act as a handler for the JSON returned object. In this last article I have removed the extra handler template, interfacing directly with the CFC.

Firstly there are some small changes to the JQuery url call.

view plain print about
1<scr/ipt src="jquery-1.2.3.js" type="text/javascript"></script>
2<scr/ipt src="select-chain.js" type="text/javascript" charset="utf-8"></script>
3<scr/ipt type="text/javascript">
4 jQuery.noConflict();
5 jQuery(function () {
6 var type = jQuery('#series');
7 var sp = jQuery('#issueno');
8 var selectedSeries = jQuery('#series').val();
9
10 type.selectChain({
11 target: sp,
12 url: 'jquerySeries.cfc?method=getIssueNos',
13 type: 'post',
14 data: { ajax: true, returnformat: 'plain', series: selectedSeries }
15 }).trigger('change');
16
17 });
18</script>

Now we are directly referencing the CFC. Simply append the function name as a url parameter. In the "DATA" element we place the data we are passing in the form of key value pairs. This will build all the values into the JQuery url, so the final URL would be:

view plain print about
1jquerySeries.cfc?method=getIssueNos&ajax=true&returnformat=plain&series=selectedSeries

At this point I was getting an parse error, until I found the 'returnFormat' value. Coldfusion will return WDDX encoded packets by default, problem was my handler was looking for Json. Adding this value will stop this.

Next we have the same form as before, there are no changes at all: (here for completeness really)

view plain print about
1<cfset variables.series = createObject("component","jquerySeries").getSeries()>
2 <form action="" method="post">
3 <select name="series" id="series">
4 <cfoutput query="variables.series">
5 <option value="#variables.series.intId#">#variables.series.varName#</option>
6 </cfoutput>
7 </select>
8 <select name="issueno" id="issueno">
9 <option></option>
10 </select>
11 <button type="submit">Submit</button>
12 </form>

Lastly there are one or two small changes to our CFC function.

view plain print about
1<cffunction name="getIssueNos" access="remote" output="false" hint="returns related series issue numbers">
2        <cfargument name="series" type="numeric" required="false" hint="Id of the publication">
3        <cfset var result = "">
4
5        <cfquery name="result" datasource="#application.ds#">
6            SELECT [intIssueNo]
7            FROM [dbo].[tbl_cn_series_issueNos]
8            where intSeriesId = <cfqueryparam value="#arguments.series#" cfsqltype="cf_sql_integer">
9        </cfquery>
10        
11        <cfsetting showdebugoutput="false">
12        <cfset ojson = createObject("component","cfjson")>
13        <cfset theresults = ojson.encode(listToArray(valuelist(result.intIssueNo)))>
14        <cfsetting enablecfoutputonly="true">
15    <cfreturn theresults>
16    </cffunction>

The access method has changed, so we add an "access=remote" value to expose the function as a service. Then we serialize the query result into JSON, and return it. This seems like a much more succinct way of doing this.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top