我正在使用Typeahead.js选择项目名称,我将从PHP / Mysql页面检索(通过Bloodhound)我的一个输入字段。
当我输入输入字段时,建议始终显示5第一行不过滤,但突出显示似乎正常工作(请参见下面的屏幕截图)。
知道我做错了吗?
var dataSource = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('item_name', 'item_code'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
//local: jsonData
remote : {
url:'function/load-item.php?item_name=%QUERY',
}
});
dataSource.initialize();
$('#item_name').typeahead({
minLength : 3,
highlight : true
}, {
limit : 25,
name : 'countries',
display : function(item) {
return item.item_name
},
source : dataSource.ttAdapter(),
suggestion : function(data) {}
});
$('#item_name').on('typeahead:selected', function (e, datum) {
console.log(datum);
$('#item_code').val(datum.item_code);
});
session_start();
include ('../include/connect.php');
$query = "SELECT item_name, item_code FROM master_item";
$return = array();
if($result = $conn->query($query)){
// fetch array
while ($row=mysqli_fetch_assoc($result)) {
$return[] = $row;
}
// free result set
$result->close();
// close connection
$conn->close();
$json = json_encode($return);
print_r($json);
}
[{
"item_name":"wrench",
"item_code":"aa"
}, {
"item_name":"compressor",
"item_code":"bb"
}, {
"item_name":"grinder",
"item_code":"cc"
}, {
"item_name":"air con",
"item_code":"dd"
}, {
"item_name":"handphone",
"item_code":"ee"
}, {
"item_name":"refreigrator",
"item_code":"ff"
}]
答案 0 :(得分:0)
将sufficient
添加到初始值设定项并将其值设置为 0 。
此外,远程url参数后代码中还有一个额外的逗号。
var dataSource = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('item_name', 'item_code'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
sufficient: 0,
remote : {
url:'function/load-item.php?item_name=%QUERY',
wildcard: '%QUERY'
}
});