1

我有一个文本字段,我想根据查询自动建议值。我有一个主文件和一个单独的文件 (getdata.cfc) 来保存我的查询。

这是我的主文件的文本字段部分:

<cfinput name="search_query" autosuggest="url:getdata.cfc?suggestvalue={cfautosuggestvalue}" maxResultsDisplay="10" showAutoSuggestLoadingIcon="true" size="10" /> 

这是 getdata.cfc 中的代码:

   <cfcomponent>
    <cffunction name="get_data" access="remote"  output="false">
        <cfargument name="suggestvalue" required="true">

        <cfquery name="get_data" datasource="#application.DSN#">
                SELECT DISTINCT myItem 
                FROM myTable
                WHERE myItem LIKE <cfqueryparam value="#suggestvalue#%"
                cfsqltype="cf_sql_varchar">
                ORDER BY myItem 
        </cfquery>

        <cfif get_data.recordCount eq 1>
            <cfreturn ",#get_data.myItem#">
       <cfelse>
            <cfreturn ValueList(get_data.myItem)>
       </cfif>
    </cffunction>
</cfcomponent>

文本字段显示得很好,但是当我输入一个单词时,没有显示自动建议值。什么都没发生。文本只是在我键入时显示。

有什么建议么?谢谢!

4

1 回答 1

2

I switched away to using jquery plugins from a lot of CF stuff, but here is an example I have that works in some old production code

<cfinput type="text" name="email" id="email" autosuggest="cfc:cfc.users.lookupEmail({cfautosuggestvalue})" maxresultsdisplayed = "25">

<cffunction name="lookupEmail" access="remote" returntype="array">
    <cfargument name="search" type="any" required="false" default="">

    <!--- Define variables --->
    <cfset var data="">
    <cfset var result=ArrayNew(1)>

    <!--- Do search --->
    <cfquery name="data"  datasource="datasource" maxrows="25" cachedwithin="#CreateTimeSpan(0,0,30,0)#">
    SELECT distinct email
    FROM users
    WHERE email LIKE  <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.search#%">
    ORDER BY email
    </cfquery>

    <!--- Build result array --->
    <cfloop query="data">
    <cfset ArrayAppend(result, email)>
    </cfloop>

    <!--- And return it --->
    <cfreturn result>
</cffunction>

maybe this helps

also make sure you have your cfform tags around your form, and make sure that your /cfide folder is mapped to your website.

looking at your code and comparing it... it may be the way your calling the cfc (filename)

try: autosuggest="cfc:getdata.get_data.({cfautosuggestvalue})"

于 2012-11-01T18:31:27.873 回答