简体   繁体   English

Twitter Bootstrap提前输入静态数据

[英]Twitter Bootstrap Typeahead With Static Data

I need to use typeahead for very large data. 对于非常大的数据,我需要使用typeahead。 Generation of data consumes 4-5 seconds. 数据生成耗时4-5秒。 So I cannot make Ajax calls each time. 所以我不能每次都拨打Ajax。 I download and cache the data upon user request. 我应用户要求下载并缓存数据。 My code is the following: 我的代码如下:

$("#build-package-list-btn").click(function(){
        $.get**JSON**("/packages", function(data){
            // data is an array
            $("#typeahead-packages").typeahead({source:data});
            console.log(data == null); // returns false
        });
    })

It gives no error but whenever I try to write to typeahead text box, it gives me the following error: 它没有任何错误,但是每当我尝试写入预输入文本框时,就会出现以下错误:

Uncaught TypeError: Cannot call method 'toLowerCase' of null bootstrap.js:1644
Typeahead.matcher bootstrap.js:1644
(anonymous function) bootstrap.js:1631
e.extend.grep jquery-1.7.2.min.js:2
Typeahead.lookup bootstrap.js:1630
Typeahead.keyup bootstrap.js:1738
e.extend.proxy.g jquery-1.7.2.min.js:2
f.event.dispatch jquery-1.7.2.min.js:3
f.event.add.h.handle.i jquery-1.7.2.min.js:3

My typeahead is like this (JADE) 我的打字像这样(JADE)

input#typeahead-packages(type="text",data-provide="typeahead")

Also in the Chrome Console I tried: 同样在Chrome控制台中,我尝试了以下操作:

 $("#typeahead-packages").typeahead({source:["abcdef","abcddd","abcccc"]});

But typeahead does not give an error but it also does not work. 但是,提前输入不会产生错误,但是也无法正常工作。 I cannot find what am I doing wrong. 我找不到我在做什么错。 I am using the 2.0.4 bootstrap. 我正在使用2.0.4引导程序。

EDIT: I changed it to getJSON from get it did not help. 编辑:我将其更改为从获取getJSON并没有帮助。 However when I construct data like this, it is working: 但是,当我像这样构造数据时,它正在工作:

data = [new String((data[0])), new String((data[5]))];

Based on your update, the problem comes from the JSON. 根据您的更新,问题来自JSON。

jQuery.getJSON() jQuery.getJSON()

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. 重要提示:从jQuery 1.4开始,如果JSON文件包含语法错误,则请求通常会静默失败。 ( source ) 来源

You should concentrate on the source of the items. 您应该专注于项目的来源。 be sure that this is an array of strings. 确保这是一个字符串数组。 Your error suggest data is a string. 您的错误提示data是字符串。

You can use the Chrome Console (or any of your choice), under the Network tab , and spy on the actual results of your AJAX request. 您可以在“ 网络”标签下使用Chrome控制台(或您选择的任何控制台),并监视AJAX请求的实际结果。 JSONLint is an interesting tool to validate your result. JSONLint是用于验证结果的有趣工具。

Then your next problem will be to update the source of an already initialized typeahead. 然后,您的下一个问题将是更新已经初始化的预输入的源。 You can use the following code : 您可以使用以下代码:

var $myTypeahead = $('#myTypeahead');
var typeaheadObj = $myTypeahead.data('typeahead');
if(typeaheadObj) typeaheadObj.source = ["abc", "cde"];

Example (jsfiddle) 例子(jsfiddle)

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

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