CF10 Jquery Autocomplete drop down names not sorting through the text the user is typing

与世无争的帅哥 提交于 2019-12-12 04:41:30

问题


I have a CF10 function that I am trying to figure out how to have the autocomplete work properly. When I start typing a name in the text box the query is dropping all the names down but not sorting through to match the characters that are typed. Is there an issue with my searchPhrase and query.term? Can anyone share some guidance into how I can fix the textbox to only drop down the names that correspond with what the user is typing?

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

        <cfquery name="query_names" datasource="dsn">
           SELECT CONCAT(assoc_last, ' ', assoc_first) AS whole_name FROM table
       WHERE assoc_status = 'ACTIVE'
    </cfquery>

    <cfset result = arrayNew(1) >

    <cfloop query="query_names">
        <cfset ArrayAppend(result, query_names.whole_name)>
    </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>

tried:

    <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 
       AND ( assoc_last like 'searchPhrase%'
                 or assoc_first like 'searchPhrase%')
       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:


This is a formatted comment. Do one thing at a time. Once something is working properly, don't change it..

Step 1 - get your query to work. If possible, do this with a database client first, then copy the sql code to ColdFusion. Otherwise, create a .cfm file with nothing except cfquery and cfdump tags.

Step 2 - get your query to work inside a function. Put your query into a function on the same .cfm file from step 1 and call it from that page. This step includes passing arguments to that function.

Step 3 - get your function to return the expected results. In your case you want an array so make sure you get one.

Step 4 - copy your function to a cfc and call it from a cfm file. Make sure you set the access to remote.

Step 5 - Call your function from javascript.



来源:https://stackoverflow.com/questions/37464505/cf10-jquery-autocomplete-drop-down-names-not-sorting-through-the-text-the-user-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!