- February 8, 2017
- Posted by: user
- Category: Uncategorized
Recently I use ColdFusion 9 for development, one of the project in that I create findcity.cfc page that contains function ‘findCity()’ to get list of cities(with zipcode) of specified state.
Code of findcity.cfc is look like:
[code:cf]
<cfcomponent displayname="findcity" output="false">
<cffunction name="findCity" access="remote" returntype="query">
<cfargument name="state" type="any" required="true">
<cfquery name="cityResult" datasource="#Application.ds#" username="#Application.UserName#" password="#Application.Password#">
select cityname, zipcode from cityZip
where statecode = <cfqueryparam value="#arguments.state#" cfsqltype="cf_sql_varchar">
group by cityname
order by cityname
</cfquery>
<cfreturn cityResult />
</cffunction>
</cfcomponent>
[/code]
I use one javascript function that send Ajax request that call ‘findCity()’ funciton.
Code look like:
[code:js]
function search_cities(id,state)
{
$.ajax({
type: "GET",
url: "/cfc/findcity.cfc?method=findCity&state="+state,
dataType: "json",
success: function(data){ // callback function that fill ‘City’ comboBox
var i=0;
objCity = $("#"+id)[0];
objCity.options.length = data.recordcount+1;
objCity.options[0].text =" -Select City-";
objCity.options[0].value = "";
objCity.options[0].selected = true;
for(i=0 ; i < data.recordcount ; ++i)
{
objCity.options[i+1].text =data.data.cityname[i];
objCity.options[i+1].value = data.data.cityname[i];
}
}
});
}
[/code]
Issue is that project build on ColdFusion 5, so that not support .cfc pages.
For that I create ‘searchCity.cfm’ page that provide the same fuctionality same as ‘findCity()’ function of findcity.cfc page
Code of searchCity.cfm is look like:
[code:cf]
<cfquery name="cityResult" datasource="#Application.ds#" username="#Application.UserName#" password="#Application.Password#">
select cityname, zipcode from cityZip
where statecode = <cfqueryparam value="#url.state#" cfsqltype="cf_sql_varchar">
group by cityname
order by cityname
</cfquery>
<!— converting Query resultSet in to json format —>
<cfset dData = cityResult>
<cfset dJSONString = "" />
<cfset recordcountKey = "recordcount" />
<cfset columnlistKey = "columnlist" />
<cfset columnlist = LCase(dData.columnlist) />
<cfset dataKey = "data" />
<cfset dJSONString = dJSONString & ‘"#recordcountKey#":’ & dData.recordcount />
<cfset dJSONString = dJSONString & ‘,"#columnlistKey#":"’ & columnlist & ‘"’ />
<cfset dJSONString = dJSONString & ‘,"#dataKey#":’ />
<cfset dJSONString = dJSONString & "{" />
<cfset colPos = 1 />
<cfloop list="#cityResult.columnlist#" delimiters="," index="column">
<cfif colPos GT 1>
<cfset dJSONString = dJSONString & "," />
</cfif>
<cfset column = LCase(column) />
<cfset dJSONString = dJSONString & ‘"’ & column & ‘":[‘ />
<cfloop from="1" to="#cityResult.recordcount#" index="i">
<cfif i GT 1>
<cfset dJSONString = dJSONString & "," />
</cfif>
<cfset dJSONString = dJSONString & ‘"’ & cityResult[column][i] & ‘"’/>
</cfloop>
<cfset dJSONString = dJSONString & "]" />
<cfset colPos = colPos + 1 />
</cfloop>
<cfset dJSONString = dJSONString & "}" />
<cfoutput>{#dJSONString#}</cfoutput>
[/code]
Now, call ‘searchCity.cfm’ page to fill ‘City’ comboBox using Ajax by setting ‘url’ attribute same as below,
[code:js]
url:"/cfc/searchCity.cfm?state="+state;
[/code]
Hope this is helpful.