twitter-bootstrap bootstrap typeahead - 更改源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13562932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 15:47:29  来源:igfitidea点击:

bootstrap typeahead - changing source

jquerytwitter-bootstrapbootstrap-typeahead

提问by Tzury Bar Yochay

I looking for a way to change sourcefor typeahed.

我正在寻找一种改变sourcetypeahed 的方法。

For instance, assume I have the following 2 lists, and depend on the case, I'd like to have the typeahead workingin with a different source.

例如,假设我有以下 2 个列表,并且视情况而定,我希望预先输入使用不同的来源。

var list1 = ["this", "is", "first", "list"],
    list2 = ["second", "list", "comes", "here"];

$("selector").typeahead({source: list1})

Then, when I do

然后,当我做

$("selector").typeahead({source: list2})

and start typing into the input box, the first list appears underneath the new one.

并开始在输入框中输入,第一个列表出现在新列表的下方。

I tried doing

我试着做

$("selector")
    .removeData()
    .typeahead({source: list2})

Yet, it has no effect.

然而,它没有效果。

回答by Samuel Caillerie

You have to update the data field associated as indicated here:

你必须更新指示相关联的数据字段在这里

$("selector").data('typeahead').source = list2;

回答by Neil.Allen

None of these answers worked for me when using the Bootstrap-3-Typeaheadlibrary. I was able to update the source by destroy the initialized typeahead and re initializing it with a new source:

使用Bootstrap-3-Typeahead库时,这些答案都不适合我。我能够通过销毁初始化的预先输入并使用新源重新初始化它来更新源:

    $(".typeahead").typeahead("destroy");
    $(".typeahead").typeahead({ source: result });

回答by KonstantinK

Unfortunately, the accepted answer didn't work for me. It could be an issue with newer versions of the library. For me, data('typeahead') isn't defined - instead there is data('ttTypeahead') and data('ttAttrs'). Neither of those has a source attribute.

不幸的是,接受的答案对我不起作用。较新版本的库可能存在问题。对我来说,data('typeahead') 没有定义——而是有 data('ttTypeahead') 和 data('ttAttrs')。这些都没有源属性。

After some code reading I came up with this: $('selector').data('ttTypeahead').dropdown.datasets[0].source = mySourceFunction

经过一些代码阅读后,我想出了这个: $('selector').data('ttTypeahead').dropdown.datasets[0].source = mySourceFunction

And it seems to work. There might be instances where more than one dataset is defined, though.

它似乎有效。但是,可能存在定义了多个数据集的情况。