我使用Alpaca窗體並從Web服務中爲窗體自動填充字段拉取值。我已經完成了如何使用這些數據作爲使用jQuery的自動完成的值,現在需要在Alpaca表單中使用這個數據源,它使用typeahead.js和Bloodhound.js。我不太確定Alpaca如何與這些其他JS庫進行交互。下面的代碼返回自動完成字段中的值數組,但當然只有匹配的值應該顯示和可選。我不確定是否需要創建一個Bloodhound實例並解析數組匹配,或者在「建議」模板中處理。迄今爲止我都試過但沒有成功。使用遠程數據與Alpaca窗體自動完成
<html>
<head>
<title>Alpaca-Autocomplete Form</title>
<link type="text/css" rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<!-- jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<!-- Bootstrap -->
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<!-- Handlebars -->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.js"></script>
<!-- Alpaca -->
<link type="text/css" href="http://code.cloudcms.com/alpaca/1.5.14/bootstrap/alpaca.min.css" rel="stylesheet" />
<script type="text/javascript" src="http://code.cloudcms.com/alpaca/1.5.14/bootstrap/alpaca.min.js"></script>
<!-- jQuery UI Support -->
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css">
<!-- typeahead.js https://github.com/twitter/typeahead.js -->
<script src="bower_components/typeahead.js/dist/bloodhound.min.js" type="text/javascript"></script>
<!-- <script src="bower_components/typeahead.js/dist/typeahead.bundle.min.js" type="text/javascript"></script> -->
<script src="typeahead-0.10.5.bundle.min.js" type="text/javascript"></script>
</head>
<body>
<div id="field7"> </div>
<script>
$("#field7").alpaca({
"schema": {
"type": "string"
},
"options": {
"type": "text",
"label": "Input Parameter DataType",
"helper": "Select the name of the input parameter data type",
"typeahead": {
"config": {
"autoselect": true,
"highlight": true,
"hint": true,
"minLength": 1,
"data": {
"q": "request.term"
}
},
"datasets": {
"type": "remote",
"name": "data",
"displayKey": function(data) {
console.log("** displayKey function called **")
return data.field_values.buckets;
},
"source": "http://smart-api.info/api/suggestion/?field=services.inputParameter.parameterDataType",
"displayKey": function(source) {
var arr = source.buckets;
var results = [];
var dataKeys = source.buckets.map(function (x) {
results.push({"value": x.key}); //expected data format for type: remote for Alpaca
return x.key // key is the name of the key to extract the value for display
});
console.log(dataKeys);
return dataKeys;
},
"templates": function(dataKeys){
var matcher = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(dataKeys, function(item){
return matcher.test(item);
}));
}
}
}
}
});
</script>
</body>
</html>
是的,這有幫助!有沒有辦法將顯示限制爲只匹配項? – tw1742
我做了一些編輯基於http://stackoverflow.com/questions/22059933/twitter-typeahead-js-how-to-return-all-matched-elements-within-a-string更改datumTokenizer和遠程預取。我的編輯必須經過同行評審,因此可能尚未顯示。 – tw1742
我嘗試了很多解決方案,我認爲你的數據源響應格式有問題,但我不知道,但是當我改變它的數據源時它很奇怪,它就像一個魅力! – vert3x