CF10查询多行以进行jquery自动完成

时间:2016-05-26 14:06:46

标签: jquery autocomplete coldfusion

我有一个CF10功能,我试图弄清楚如何显示自动完成的assoc_last,assoc_first(姓氏,名字)。我能够显示名字或姓氏但由于某种原因无法弄清楚如何设置数组以接受这两者。有人能帮我指导我可能出错的地方吗?

<cffunction name="queryNames" access="remote" secureJSON="false">
        <cfargument name="searchPhrase">

        <cfquery name="query_names" datasource="dsn">
           SELECT assoc_userid, assoc_last, assoc_first FROM table
           WHERE assoc_status = 'ACTIVE' 
        </cfquery>

        <cfset result = arrayNew(1) >

        <cfloop query="query_names">
            <cfset ArrayAppend(result, query_names.assoc_last)>
        </cfloop>

        <cfreturn result>
    </cffunction>

JS

<script type="text/javascript">
        $(document).ready(function() {

            $("#name").autocomplete({
                source: function(query, response) {
                    $.ajax({
                        url: "redirects/autocomplete.cfc?method=queryNames&returnformat=json",
                        dataType: "json",
                        data: {
                            searchPhrase: query.term
                        },
                        success: function(result) {
                            response(result);
                        }
                    });
                }
            });
        });
    </script>

尝试:

        

    <cfquery name="query_names" datasource="dsn">
       SELECT CONCAT(assoc_last, ' ', assoc_first) AS `whole_name` FROM table
       WHERE assoc_status = 'ACTIVE' 
       and assoc_last NOT LIKE 'Test%' 
       and len(assoc_last) > 0 
       ORDER BY assoc_last
    </cfquery>

    <cfset result = arrayNew(1) >

    <cfloop query="query_names">
        <cfset ArrayAppend(result, query_names.whole_name)>
    </cfloop>

    <cfreturn result>
</cffunction>

1 个答案:

答案 0 :(得分:0)

<cffunction name="queryNames" access="remote" secureJSON="false">
        <cfargument name="searchPhrase">
            <cfquery name="query_names" datasource="dsn">
               SELECT assoc_last + ' ' + assoc_first as whole_name, assoc_userid  FROM table
               WHERE assoc_status = 'ACTIVE'
               AND ( assoc_last like 'searchPhrase%'
                     or assoc_first like 'searchPhrase%')
           </cfquery>
        <cfset resultSTRUCT = {
                'resultcount' = 0,
                'items' = arraynew(1)
            }/>
      <cfif query_names.recordCount>
        <cfset resultSTRUCT.resultcount = query_names.recordcount/>
        <cfloop query="query_names">
             <cfset arrayappend(resultSTRUCT.items, { 
                        "whole_name" = "#whole_name#"
                        ,"id" = "#assoc_userid#"
                    })/>
        </cfloop>
      </cfif>
     <cfreturn result>

</cffunction>