mysql - Typeahead.js 未填充搜索框

标签 mysql node.js typeahead.js

我正在关注这个webpage使用 Typeahead.js 从 MYSQL 数据库填充输入框的教程

Server.js

const pool=mysql.createPool({
    connectionLimit:10,
    host:'localhost',
    user:'user',
    password:'password',
    database:'table'
})
router.get("/",(req,res)=>{
    res.render("home")
})




router.get('/search',function(req,res){
    pool.query('SELECT tableName from parts where tableName like "%'+req.query.key+'%"',
    function(err, rows, fields) {
    if (err) throw err;
    var data=[];
    for(i=0;i<rows.length;i++)
    {
    data.push(rows[i].tableName);
    }
    res.end(JSON.stringify(data));
    });
    });

home.ejs

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="typehead.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote: 'http://localhost:3000/search?key=%QUERY',
limit: 10
});

});

</script>
</head>
<body>
<input class="typeahead tt-query" spellcheck="false" autocomplete="off" name="typeahead" type="text" />

typeahead.js 库已正确加载,我在 google chrome 控制台中没有看到任何错误。我的数据库也连接正常,因为我有其他访问连接池的路由,该路由工作正常。主路由正在加载搜索栏,但是当我在其中键入 mySQL 数据库中存在的表名称时,它不会提供任何自动完成。

最佳答案

基于此处的示例:http://twitter.github.io/typeahead.js/examples/ ,看起来您缺少构建一个 Bloodhound 对象来传递给 typeahead 的 Source 属性。

他们的例子:

var bestPictures = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../data/films/post_1960.json',
  remote: {
    url: '../data/films/queries/%QUERY.json',
    wildcard: '%QUERY'
  }
});

$('#remote .typeahead').typeahead(null, {
  name: 'best-pictures',
  display: 'value',
  source: bestPictures
});

对于你的脚本,我会尝试这个更改:

<script type="text/javascript">
    $(document).ready(function(){
$('input.typeahead').typeahead({
  name: 'typeahead',
  source: new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: '../data/films/post_1960.json',
    remote: {
      url: '-http://localhost:3000/search?key=%QUERY',
      wildcard: '%QUERY'
    }
  }),
  limit: 10
});

});

</script>

关于mysql - Typeahead.js 未填充搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55203152/

相关文章:

javascript - 为 Node CommonJS 模块创建不可变类

node.js - 无法在 Emacs 中启动 Tern 服务器

c# - 输入第二个字符后 TypeAhead 自动完成功能不起作用并且 css 看起来已损坏

javascript - 来自 Ajax 源的 Twitter Typeahead/Bloodhound 建议 : how manage multiple values?

mysql - mysql中重复的外键

php - 选择其中日期 = 当前日期

javascript - 为什么在 Node.js 中使用 util.inherits 和 .call 进行继承

jquery - 更新 Twitter 预输入的每个按键上的 JSON

mysql - 在 Ubuntu 16.04 上的 mysql 5.6 中通过配置文件设置变量

javascript - 使用 Ajax 如何将表单中的下拉菜单选择值与数据库中的记录相匹配,并获取关联的记录/行以预填充表单?