We help IT Professionals succeed at work.

autosuggest & cfinput

I have a CFINPUT text box and when I use autosuggest, it doesn't pick up %#textvalue#%.
from my query field. How can I make it search for the entered text value in the entire query field? I would like to do it without submitting the form.

Suppose if i say 'Army', it should search for the word 'Army' in my entire description field and pick all records which have the word 'Army' at any place in the description field. Please help.
Comment
Watch Question

Most Valuable Expert 2015

Commented:
Can you post your code for the autosuggest and query?

Author

Commented:
<cfquery datasource="#TIMPOLookupDSN#" name="q1">
 select Facility_Name
 from DMIS
</cfquery>

<TR>
<TD align="right" nowrap>Facility Name:</TD>
<TD>
<cfinput  name="facname" type="text" autosuggest="#valuelist(q1.Facility_Name)#" maxresultsdisplayed="100">
</TD>
</TR>

Author

Commented:
I also tried the below code and got the error

The path to the CFC must be specified as a full path, or as a relative path from the current template, without the use of mappings.  
 
test.cfm

<TR>
<TD align="right" nowrap>Facility Name:</TD>
<TD>
<cfinput type="text"
    name="facname"
    autosuggest="cfc:dmis1.getFacData({cfautosuggestvalue})"
 maxresultsdisplayed="100">
</TD>
</TR>

dmis1.cfc

<cfcomponent displayname="dmis1" output="yes">
      <cffunction name="getFacData" returntype="query" access="remote">
         <cfargument name="cfautosuggestvalue" type="string" required="yes" default="">
    <cfset var cfautosuggestvalue="">
    <cfquery datasource="#TIMPOLookupDSN#" name="facname">
       select Facility_Name
       from DMIS
     where Facility_Name like '%#ARGUMENTS.cfautosuggestvalue#%'    
       order by Facility_Name
   </cfquery>
   <cfreturn facname>
 </cffunction>
</cfcomponent>
Most Valuable Expert 2015
Commented:
    > it should search for the word 'Army' in my entire description field
By default autosuggest only returns values that start with the entered text. To match the string anywhere in the text, you have to customize the underlying library's settings.  

      > autosuggest="#valuelist(q1.Facility_Name)#"
But I'm not sure it works with a hard coded list.  I've only used it with a CFC:

Form
* Note: CF can be VERY picky about the placement of the <html>,<head> and <body> tags.
<html>
<head>
<script type="text/javascript">
    var setFilter = function(){
        var suggest = ColdFusion.objectCache[ 'facname' ];
        suggest.queryMatchContains = true;
    };    
</script>
</head>
<body>
<cfform>
Facility Name:
<cfinput  name="facname" type="text" 
        autosuggest="cfc:YourComponent.getFacilitiesByName( {cfautosuggestvalue} )"
        maxresultsdisplayed="100" />
</cfform>
</body>
</html>
<cfset ajaxOnLoad("setFilter")>

Open in new window


YourComponent.cfc  
* Tested under CF9. Change the datasource name
<cfcomponent output="false">
    <cfset variables.DSN ="Your_Datasource_Name_Here">
    
    <cffunction name="getFacilitiesByName" access="remote" output="false" returnType="any">  
        <cfargument name="facilityName" type="any" default="">      
    
        <cfset var q1  = "">
        <cfset var resultArray = []> 
    
        <cfquery name="q1" datasource="#variables.DSN#">
            SELECT  Facility_Name
            FROM     DMIS
            WHERE  Facility_Name LIKE <cfqueryparam value="%#arguments.facilityName#%" cfsqltype="cf_sql_varchar">
            ORDER BY Facility_Name
        </cfquery>
        <cfloop query="q1">
            <cfset arrayAppend(resultArray, q1.Facility_Name)>
        </cfloop>
    
        <cfreturn serializeJSON(resultArray)>
    </cffunction>
</cfcomponent>

Open in new window

Most Valuable Expert 2015

Commented:
> The path to the CFC must be specified as a full path, or as a relative path from the current template, without the use of mappings.  

That just means there's something wrong with the path and CF can't find the CFC the way it's referenced. Is the "dmis1.cfc" file in the same folder as "test.cfm"?

     autosuggest="cfc:dmis1.getFacData({cfautosuggestvalue})"

Author

Commented:
Your code works like a champ...You are really great. Thanks a lot.

I will be posting another question soon.
Most Valuable Expert 2015

Commented:
Welcome :)