7 Replies Latest reply on Nov 9, 2010 11:50 AM by NettlesD

    JQuery and CFC

    NettlesD

      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",