jquery ui autocomplete没有返回数据

时间:2015-05-20 18:32:50

标签: javascript jquery coldfusion

我遇到自动填充问题。我有以下代码;

<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>

$(function() {
    $('.namesuggestclass').autocomplete({
        source: function(request, response){
            $.ajax({
                url: 'cfc/basic.cfc?method=getIndivs',
                dataType: "json",
                data: {
                    searchterm: request.term
                },
                error: function(xhr, textStatus, errorThrown) {
                    alert(errorThrown);
                },
                success: function(data){
                    response(data);
                }
            })
        },
        minLength: 3,
        onSearchError: function (query, jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        },
        select: function(event, ui) {
            alert(ui.item.ISRECNUM);
        }
    });
});



<cfinput name="namesuggest" class="namesuggestclass">

我在Coldfusion 10服务器上运行。当我输入3个或更多字符时,我得到3个空行。

我的cfc是:

<cffunction name="getIndivs" access="remote" HINT="Get All Resources" returnformat="json" output="false" >
        <cfargument name="searchterm" required="True" default="">
        <cfset local.returnArray =ArrayNew(1)>
        <cfquery name="local.get_Indivs" datasource="#request.dsn#" maxrows=20>
            SELECT ui.id as isrecnum, UI.lastname + ', ' + UI.firstname as iname , c.name as companyname, c.city as companycity, UI.Status_Flag
            FROM users_info AS UI left join
            companies as c on c.id = ui.current_company_number
            WHERE 0=0
            And ui.status_Flag != 'D'
            <cfif trim(arguments.searchterm) NEQ "">
                AND UI.lastname + ' ' + ui.firstname + ' ' + c.name like
                <cfqueryparam cfsqltype="cf_sql_varchar" value="#left(trim(arguments.searchterm),255)#%">
            <cfelse>
              AND UI.lastname = 'abcdefg'  <!--- return empty query   vjl  2011/07/27 --->
            </cfif>
            ORDER BY lastname, firstname
        </cfquery>
        <cfloop query="local.get_Indivs">
            <cfset indivStruct = structNew() />
            <cfset indivStruct['ISRECNUM'] = ISRECNUM />
            <cfset indivStruct['INAME'] = INAME />
            <cfset indivStruct['COMPANYNAME'] = COMPANYNAME />
            <cfset indivStruct['COMPANYCITY'] = COMPANYCITY />
            <cfset arrayAppend(local.returnArray,indivStruct) />
        </cfloop>
        <CFRETURN local.returnArray>
    <!--- <CFRETURN local.get_Indivs> --->
    </cffunction>

cfc调用返回的数据是; [{&#34; ISRECNUM&#34;:77137,&#34; INAME&#34;:&#34; Lan,Donald&#34;,&#34; COMPANYNAME&#34;:&#34;公司A&#34; ;,&#34; COMPANYCITY&#34;:&#34;达拉斯&#34;},{&#34; ISRECNUM&#34;:240316,&#34; INAME&#34;:&#34; Lan,Mike&# 34;,&#34; COMPANYNAME&#34;:&#34;公司B&#34;,&#34; COMPANYCITY&#34;:&#34;卡尔加里&#34;}, 等..............]

编辑:我更新了我的代码和数据。我无法看到我与工作示例的不同之处。

2 个答案:

答案 0 :(得分:1)

正如vilsad所说:json数据格式错误。您尝试重建从数据库获取的数据,jqueryui-autocomplete所需的json格式为:

label:xxxx  --display as select's title 
value:xxxx  --as select element's value

在php中,我格式化我的数据:

$ds=$db->query('select id,title from products whrere title like'.keyword.'%');
$result=array(
    'label'=>$ds['title'],
    'value'=>$ds['id']
);
echo json_encode($result);

原谅我的英语

答案 1 :(得分:0)

似乎你的json对象不像autocomplete插件那样预期,要么在服务器端代码中纠正它,要么使用自动完成插件_renderItem在客户端进行更改。

http://api.jqueryui.com/autocomplete/#method-_renderItem