我在让引导程序预先输入以与 AJAX 源一起正常工作时遇到了很多麻烦。
如果我提醒正在返回的数组,它非常好,即使我对它进行硬编码来测试它。当您输入近似匹配时,它似乎大多数时候什么都不返回,或者只返回数组中的几个项目。
这是我目前所拥有的:
$('#companyNameInput').typeahead({
source: function(query, process){
$.ajax({
url: ROOT+'Record/checkCompanyName',
async: false,
data: 'q='+query,
type: 'POST',
cache: false,
success: function(data)
{
companiesFinal = [];
map = {};
companies = $.parseJSON(data);
$.each(companies, function(i, v){
map[v.name] = v.id;
companiesFinal.push(v.name);
})
}
})
process(companiesFinal);
// return ['test1', 'test2'] This works fine
return companiesFinal;
}
有谁知道为什么这可以正常工作?
这是从我的 PHP 脚本返回的对象数组的示例。ID 为 1 和 1216 的对象显示在 typeahead 下拉列表中,但其他对象都没有。我看不到任何模式或线索,说明为什么只有这些会显示而不是其他。
[
{
"id": "1265",
"score": "40",
"name": "LMV AB"
},
{
"id": "10834",
"score": "33",
"name": "Letona"
},
{
"id": "19401",
"score": "33",
"name": "Lewmar"
},
{
"id": "7158",
"score": "33",
"name": "Lazersan"
},
{
"id": "3364",
"score": "33",
"name": "Linpac"
},
{
"id": "1216",
"score": "33",
"name": "L H Evans Limted"
},
{
"id": "1",
"score": "33",
"name": "LH Evans Ltd"
},
{
"id": "7157",
"score": "33",
"name": "Lazersan"
}
]
最后是过去的数组process(companiesFinal)
:
["LMV AB", "Letona", "Lewmar", "Lazersan", "Linpac", "L H Evans Limted", "LH Evans Ltd", "Lazersan"]
有人有任何线索吗?我仍然完全不知道为什么这仍然不起作用:(
$('#companyNameInput').typeahead({
source: function(query, process){
companyTOut = setTimeout(function(){
return $.ajax({
url: ROOT+'Record/checkCompanyName',
data: 'q='+query,
type: 'POST',
dataType: 'json',
cache: false,
success: function(data)
{
var count = 0;
var companiesFinal = [];
map = [];
$.each(data, function(i, v){
map[v.name] = [v.id, v.score];
companiesFinal.push(v.name);
})
process(companiesFinal);
}
})
}, 250)
},
minLength: 2,
highlighter: function(item)
{
$('#companyNameInput').closest('.control-group').removeClass('success')
companyLocked = false;
return '<span class="unselectable" title="'+map[item].score+'">'+item+'</span>';
},
updater: function(item)
{
selectedEntityId = map[item][0];
selectedCountryScore = map[item][1];
lockCompany(selectedEntityId);
return item;
}
});
$output .= '['.$output;
foreach($results as $result) {
$output .= '{"id":"'.$result['id'].'",';
$output .= '"score":"'.$result['score'].'",';
$output .= '"name":'.json_encode($result['name']).'},';
}
header('Content-Type: application/json');
echo substr($output, 0, strlen($output)-1).']';
“parm”的控制台输出:
[Object, Object, Object, Object, Object, Object]
0: Object
id: "25024"
name: "part"
score: "75"
__proto__: Object
1: Object
id: "15693"
name: "pari"
score: "75"
__proto__: Object
2: Object
id: "28079"
name: "Pato"
score: "50"
__proto__: Object
3: Object
id: "18001"
name: "PASS"
score: "50"
__proto__: Object
4: Object
id: "15095"
name: "PSR"
score: "33"
__proto__: Object
5: Object
id: "22662"
name: "PRP"
score: "33"
__proto__: Object
length: 6
__proto__: Array[0]