我正在使用名为" Jquery-ui-multisearch"它根据您提供的数组或外部源(ajax / api / etc)在输入元素中提供自动完成功能。在此处找到:http://bseth99.github.io/jquery-ui-multisearch/index.html
我目前正在尝试使用该插件根据ajax数据库调用返回的字符串数组提供自动完成建议,但我无法弄清楚该插件想要看到的格式。
以下是插件的初始化示例。
<style>
#customheader a {
font-size: 60px;
font-family: lato light, 'cantarell';
color: #737373;
text-transform: uppercase;
font-weight: normal!important;
letter-spacing: 0.07em;
}
#customheader {
margin: 7% 0 2% 0;
padding: 0 0 3.5% 0;
border-bottom: 1px solid #cccccc;
}
#customheader a:hover {
color: #000000!important;
}
</style>
<center>
<div id='customheader'>
<a href='http://www.blankesque.com'>Blankesque</a>
</div>
</center>
以下是我的代码:
$(function() {
$("#myMultiSearch").multisearch({
source: function ( term, callback ) { ... } //A function can implement a data search and should call the passed in callback with the results.
});
});
看起来与他们在这里做的相似:http://bseth99.github.io/jquery-ui-multisearch/examples/movies.html - 看起来像这样:
$("#search").multisearch({
source: function (term, callback) {
$.ajax({
type: 'POST',
url: postUrl,
data: {searchterm:term},
})
.done(function(data) { //data == ["abc","abcd","abcde"];
callback(data);
});
});
我也尝试过回调(data.toJSON());或者将输出数据更改为{&#34; abc&#34;,&#34; abcd&#34;,&#34; abcde&#34;}或(&#34; abc&#34;,&#34) ; abcd&#34;,&#34; abcde&#34;)等等。所有结果都会导致未定义的结果或一个没有任何结果的框。
那么如何将返回的项目数组传递给multisearch函数,以便它可以在结果中显示它?
如果我需要更具体地沟通,请告诉我 - 我知道,当提问者不是很清楚时,尤其是在第三方插件上难以诊断问题。
答案 0 :(得分:1)
上个月我在github发布了这个问题,作者在这里回复了我:https://github.com/bseth99/jquery-ui-multisearch/issues/2
我不得不从php中获取数组并从中创建一个新的哈希数组。这是我的代码。
.done(function(data) {
var jsonparse = JSON.parse(data);
var hashArray = [];
jsonparse.forEach(function(entry) {
hashArray.unshift({name: entry});
});
//console.log(hashArray);
callback(hashArray); //Same format as the line below.
//callback([{ name: "item1" },{ name: "item2" }]); //This works.
})
感谢您的帮助!