javascript - Typeahead.js 建议未被过滤

标签 javascript php mysql typeahead.js bloodhound

我正在使用Typeahead.js选择我从 PHP/Mysql 页面检索(通过 Bloodhound)的项目名称,作为我的输入字段之一。

当我在输入字段中键入时,建议始终显示 5 第一行没有过滤,但是突出显示似乎工作正常(请参见下面的屏幕截图)。

enter image description here

知道我做错了什么吗?

<小时/>

我的JS:

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);
}); 

我的 PHP:

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);
}

我的 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"
}]

最佳答案

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'
  }
});

关于javascript - Typeahead.js 建议未被过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35475710/

相关文章:

javascript - 使用 Javascript 将可扩展表导出为 CSV

javascript - 需要读取某个时区的纪元时间,然后将其转换为用户时区

php - 如何在Ubuntu上安装Xdebug?

Wordpress 中的 PHPSpreadsheet 使用纯文本 PHP 生成损坏的文件

PHP session 问题,请帮忙

mysql - SQL Server 迁移助手 (SSMA) : ERROR [22018] [MySQL][ODBC 5. 3(a) 驱动程序][mysqld-5.1.51-社区]

mysql - 没有从存储过程中获取行

javascript - 在 for 循环中错误地传递回调参数

php - 错误! : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

javascript - 如何检测浏览器播放器是否可以处理视频格式