简体   繁体   English

如何在typeahead.js中读取json数据?

[英]How to read json data in typeahead.js?

I want to read my data in JSON file then using it with typeahead.js here's a link 我想读取JSON文件中的数据,然后将其与typeahead.js结合使用,这是一个链接

if some one write any word like EnglishWords [Best] it will show him/she list all of the words that started with best or like best and select the word then show the description of the word but I have no any idea because I am new in javascript and json thanks a lot :) 如果有人写下任何单词,例如EnglishWords [Best],它将显示他/她列出所有以best或like best开头的单词,然后选择该单词,然后显示该单词的描述,但我不知道,因为我是新手在JavaScript和JSON非常感谢:)

here's example of my json data file 这是我的json数据文件的示例

    var Words = [
{"EnglishWords":"Best","Description":"an expression of good some this"},
 {"EnglishWords":"Best wishes","Description":"an expression of hope"},
{"EnglishWords":"Application","Description":"Computing a program or piece of software"},
-
-
-
-
-
-

];

It looks like you have two separate things going on. 看来您有两件事正在发生。 One of is the autocomplete and the other some kind of word/phrase definition? 一种是自动完成功能,另一种是单词/短语定义?

typeahead.js is only going to address the first part but it seems easy enough. typeahead.js只会解决第一部分,但似乎很容易。 Adapting the example from the github page, you'd want something like: 改编自github页面的示例,您将需要以下内容:

$('input.englishwords').typeahead({
  name: 'words',
  local: ['application', 'best', 'best wishes']
});

EDIT: 编辑:

You'll have to parse your JSON object into an array or into an object that typeahead wants. 您必须将JSON对象解析为数组或typeahead输入的对象。 Something like: 就像是:

var typeList = [];

for (var i = 0; i < Words.length; i++) {
    typeList.push(Words[i].EnglishWords);
}

then:

$('input.englishwords').typeahead({
  name: 'words',
  local: typeList
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM