Skip to main content
Inspiring
November 9, 2010
Question

JQuery and CFC

  • November 9, 2010
  • 1 reply
  • 1366 views

I have a JQuery Autocomplete working with a *.cfm file but I'm struggling trying to convert that file to a CFC.  I'm sure I'm close but could use some pointers so that I know what't going on.  Or maybe the cfc is totally wrong and I'm way off base.

Lookup.cfm

<cfset Results = [] />
<cfset LookupInformation = "" />
   
<cftry>
  <cfquery name="Lookup">
  select idnumber,
         lastname,
         firstname,
         middlename,
         suffix,
         tagname,
         city,
         state
    from Registrant
      where lower(LastName) like <cfqueryparam value="#LCase(URL.term)#%" cfsqltype="cf_sql_varchar">
        order by LastName
  </cfquery>
     
  <cfcatch type="any">
  </cfcatch>
</cftry>

<cfloop query="Lookup">
  <cfset LookupInformation = "(" & idnumber & ")" & " "
                             & lastname & ", "
        & firstname & " "
        & middlename & " "
        & suffix & " "
        & tagname & " "
        & city & " "
        & state />
  <cfset LookupStruct = StructNew() />
  <cfset LookupStruct["id"] = idnumber />
  <cfset LookupStruct["firstname"] = firstname />
  <cfset LookupStruct["middlename"] = middlename />
  <cfset LookupStruct["lastname"] = lastname />
  <cfset LookupStruct["suffix"] = suffix />
  <cfset LookupStruct["tagname"] = tagname />
  <cfset LookupStruct["city"] = city />
  <cfset LookupStruct["state"] = state />
  <cfset LookupStruct["label"] = LookupInformation />
  <cfset LookupStruct["value"] = lastname />

  <cfset ArrayAppend(Results, LookupStruct) />
</cfloop>

<cfoutput>
#serializeJSON(Results)#
</cfoutput>

Lookup.cfc

<cfcomponent displayname="Lookup" output="no">
 
  <cffunction name="Lookup" access="remote" returntype="array" output="no">
    <cfargument name="Term" type="string" required="false">
   
    <cfset var Results = [] />
    <cfset var LookupInformation = "" />
   
    <cftry>
      <cfquery name="Lookup">
      select IDnumber,
             LastName,
             FirstName,
             MiddleName,
             Suffix,
             Tagname,
             City,
             State
        from Registrant
          where lower(LastName) like <cfqueryparam value="#LCase(Term)#%" cfsqltype="cf_sql_varchar">
            order by LastName,
                     FirstName
      </cfquery>
     
      <cfcatch type="any">
        <cfset arrayAppend(Results, "No results could be found.")>
      </cfcatch>
    </cftry>
     
    <cfif Lookup.recordcount is not 0>
      <cfset LookupInformation = "(" & IDnumber & ")" & " "
                                 & LastName & ", "
            & FirstName & " "
            & MiddleName & " "
            & Suffix & " "
            & Tagname & " "
            & City & " "
            & State />
 
      <cfset LookupStruct = StructNew() />
      <cfset LookupStruct["id"] = IDnumber />
      <cfset LookupStruct["firstname"] = FirstName />
      <cfset LookupStruct["middlename"] = MiddleName />
      <cfset LookupStruct["lastname"] = LastName />
      <cfset LookupStruct["suffix"] = Suffix />
      <cfset LookupStruct["tagname"] = Tagname />
      <cfset LookupStruct["city"] = City />
      <cfset LookupStruct["state"] = State />
      <cfset LookupStruct["label"] = LookupInformation />
      <cfset LookupStruct["value"] = LastName />

      <cfset ArrayAppend(Results, LookupStruct) />
    </cfif>
   
    <cfreturn Results>
  </cffunction>
 
</cfcomponent>

Could someone point out what I'm doing wrong?  Wrong returnType?  I need to add a serializeJSON to the output?

With Lookup.cfm

$("#RegistrationLastName").autocomplete({
                source: "lookup.cfm",

With Lookup.cfc

$("#RegistrationLastName").autocomplete({
                source: /CFC/Lookup.cfc?method=Lookup&returnFormat=JSON",

This topic has been closed for replies.

1 reply

cfjedimaster
Inspiring
November 9, 2010

What do you see when you use Firebug and compare the two results?

NettlesDAuthor
Inspiring
November 9, 2010

Haven't used Firebug.  The Lookup.cfm file returns results on the page.  The Lookup.cfc file doesn't return anything and reports the IDNumber is an error.  Not sure what that is about.

cfjedimaster
Inspiring
November 9, 2010

Check this video:

http://insideria.com/2009/06/quick-video-example-of-firebug.html

It tells you how to use Firebug to debug the responses sent back from

the server.