2014-10-27 35 views
0

我在我的表單中爲我的grails應用程序中的大部分字段中實現了自動完成。來自DB的JQuery自動完成

目前我有這樣的代碼(如下圖),讓我給他們鍵入將建議用戶手動輸入值,在這個例子中,我充滿兩種自動完成兩個字段的值:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
<script> 
$(function() { 

$("#parameterName").autocomplete({ 
    source: ["HelloHi", "Moose"] 
}); 
$("#defaultValue").autocomplete({ 
    source: ["Blah", "Bleh"] 
}); 
}); 

我的問題是,我需要改變什麼,以便我不必手動輸入值,我可以簡單地獲取它從數據庫的列中檢索所有值,並使用它們自動提示/完成?

任何幫助,將不勝感激。

UPDATE:

我在form.gsp功能:

<script> 
$(function() { 

$("#parameterName").autocomplete({ 
    source: '${g.createLink(controller: 'templateInput', action: 'suggestedParameterNames')}' 
    }); 
}); 
</script> 

我有我的控制器templateInput功能:

def suggestedParameterNames() { 
    def suggestions = templatingService.getSuggestedParamNameValues() 

    render suggestions as JSON 
} 

我已經在我的功能服務:

def getSuggestedParamNameValues(){ 
    def sql = new Sql(dataSource) 
    def row = sql.rows("select distinct parameter_name from template_input") 

    return row 
} 

現在我遇到的問題是,當我輸入字段時,它會返回列中的每個值。我想要它,所以如果我輸入單詞'card',它只會提示以'card'開頭的值。

任何想法?

回答

1

autocomplete plugin docs包括下面的例子

HTML:

<input type="text" name="country" id="autocomplete"/> 

阿賈克斯查找:

$('#autocomplete').autocomplete({ 
    serviceUrl: '/autocomplete/countries', 
    onSelect: function (suggestion) { 
     alert('You selected: ' + suggestion.value + ', ' + suggestion.data); 
    } 
}); 

在你的情況,而不是硬編碼返回建議列表中的網址你應該使用g.createLink標籤,例如

serviceUrl: '${g.createLink(controller: "autocomplete", action: "countries")}', 
+0

我認爲這個問題是關於jQuery UI自動填充小部件http://jqueryui.com/autocomplete而不是自動完成插件。 – MattZ 2014-10-28 14:42:32

+0

我已更新原始帖子,請檢查。 – Jamie 2014-10-28 15:31:50