Javascript Autocomplete使用羊驼表单显示键和值
我正在使用羊驼表单生成一个表单,其中一个字段将自动完成。我正在测试来自的示例7,以了解其工作原理。但是,在我的表单中,自动完成在羊驼站点上显示为{value:Cloud CMS}与Cloud CMS}。我还尝试将自动完成值直接指定为数组。下面是我的代码,注意typeahead.js是本地安装的Javascript Autocomplete使用羊驼表单显示键和值,javascript,autocomplete,typeahead.js,alpacajs,Javascript,Autocomplete,Typeahead.js,Alpacajs,我正在使用羊驼表单生成一个表单,其中一个字段将自动完成。我正在测试来自的示例7,以了解其工作原理。但是,在我的表单中,自动完成在羊驼站点上显示为{value:Cloud CMS}与Cloud CMS}。我还尝试将自动完成值直接指定为数组。下面是我的代码,注意typeahead.js是本地安装的 <html> <head> <title>Alpaca-Autocomplete Form</title> <
<html>
<head>
<title>Alpaca-Autocomplete Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<link type="text/css" rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<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://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.js"></script>
<script type="text/javascript" src="http://code.cloudcms.com/alpaca/1.5.14/bootstrap/alpaca.min.js"></script>
<!-- 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>
</head>
<body>
<div id="field7"> </div>
<script>
var companies = ["Cloud CMS", "Amazon", "HubSpot"];
$("#field7").alpaca({
"schema": {
"type": "string"
},
"options": {
"type": "text",
"label": "Company Name",
"helper": "Select the name of a cloud computing company",
"typeahead": {
"config": {
"autoselect": true,
"highlight": true,
"hint": true,
"minLength": 1
},
"datasets": {
"type": "local",
"source": companies
// "source": function(query) {
// var companies = ["Cloud CMS", "Amazon", "HubSpot"];
// var results = [];
// for (var i = 0; i < companies.length; i++) {
// var add = true;
// if (query) {
// add = (companies[i].indexOf(query) === 0);
// }
// if (add) {
// results.push({
// "value": companies[i]
// });
// }
// }
// return results;
// }
}
}
}
});
</script>
</body>
</html>
我试着玩弄你的代码,问题是你正在使用的typeahead版本。我把这个版本改成了另一个版本,它工作了,试着使用这个版本,告诉我它是否工作
祝您愉快。如果您想使用最新版本的Typeahead,这里有另一个解决方案:
$("#field7").alpaca({
"schema": {
"type": "string"
},
"options": {
"type": "text",
"id": "companyField",
"label": "Company Name",
"helper": "Select the name of a cloud computing company"
}
});
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var companies = ["Cloud CMS", "Amazon", "HubSpot"];
$('#companyField').typeahead({
hint: true,
highlight: true,
minLength: 2
}, {
name: 'companies',
source: substringMatcher(companies)
});
您必须首先在字段中添加名称或id,并从alpaca代码中删除typeahead配置,然后使用typeahead提供的代码将自动补全应用于字段
如果要将该方法用于早期版本的typeahead,则必须更改substringMatcher函数,如下所示:
// ...
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push({
value: str
});
}
});
// ...
这里有一个例子。
使用这种技术,我仍然存在一些样式问题,但我认为有一个解决方法。是的,这就解决了问题-谢谢!。我使用bower install typeahead.js进行安装,是什么促使你尝试不同的版本?很简单,我只是想在图书馆的官方网站上看看他们使用的版本。顺便说一句,我在工作中使用羊驼已经快一年了,我发现它非常有用,而且使用起来也很简单,但有时在定制方面……我非常讨厌它,因为我找不到问题的答案,加上它的github社区不是很活跃。无论如何,我并不是要你尝试其他替代方案或本机代码来生成你的表单,坚持使用它,它非常有用,提供了很多东西,你可以从中学到很多东西:关于这一点有什么想法吗