gpt4 book ai didi

javascript - Bootstrap Typeahead 与 AJAX 源 : not returning array as expected

转载 作者:行者123 更新时间:2023-11-29 14:57:37 25 4
gpt4 key购买 nike

我在让 Bootstrap 预先输入以与 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]

最佳答案

更新 2

啊,您的服务返回的项目实际上与查询不匹配 parm .您的预输入查询是 'parm' , 与返回的结果都不匹配。您可以覆盖 matcher typeahead 插件使用的函数,见 docs .只需将其实现为 return true匹配您的服务返回的所有结果。

更新 1

这是一个更新版本,将名称映射到一个 id,以后可以使用。一个 jsfiddle is available .

var nameIdMap = {};

$('#lookup').typeahead({
source: function (query, process) {
return $.ajax({
dataType: "json",
url: lookupUrl,
data: getAjaxRequestData(),
type: 'POST',
success: function (json) {
process(getOptionsFromJson(json));
}
});
},
minLength: 1,
updater: function (item) {
console.log('selected id'+nameIdMap[item]);
return item;
}
});

function getOptionsFromJson(json) {
$.each(json, function (i, v) {
nameIdMap[v.name] = v.id;
});

return $.map(json, function (n, i) {
return n.name;
});
}

原始答案

您需要使调用异步并调用 process成功回调中的回调,如下所示:
$('#companyNameInput').typeahead({
source: function (query, process) {
$.ajax({
url: ROOT + 'Record/checkCompanyName',
// async: false, // better go async
data: 'q=' + query,
type: 'POST',
cache: false,
success: function (data) {
var companiesFinal = ... // snip
process(companiesFinal);
}
})
}
});
return ['test1', 'test2'];有效,因为源函数基本上设置为:
// do ajax stuff, but do nothing with the result
// return the typeahead array, which the typeahead will accept as the result:
return ['test1', 'test2'];

笔记

有一条线要填 companiesData :
var companiesFinal = return $.map(data, function (n, i) { n.name; });

您可能希望使用 var 声明变量;否则它们将具有全局范围,这会咬你。

关于javascript - Bootstrap Typeahead 与 AJAX 源 : not returning array as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com