gpt4 book ai didi

javascript - 在 typeahead.js 中使用预取过滤器(使用基本选项)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:59 26 4
gpt4 key购买 nike

我正在使用 typeahead.js .我正在使用预取选项,需要解析返回的数据。它没有抛出错误;它什么也没做。我找过示例,但没有一个使用预取过滤器选项。

Link to the prefetch documentation

我的代码(不起作用但不抛出任何错误):

$('#autocomplete').typeahead('destroy').typeahead( {                                
name: 'organizations',
prefetch: {
url: 'jsoncall',
filter: function() {
// Blatant hardcoded return value
return ['test-a','test-b','test'c];
}
}
}
);

我的 HTML:

<input id="autocomplete" class="large-12" autocomplete="off" type="text" placeholder="Enter school name">

我的困惑:

0________0

最佳答案

您可以通过 filter 函数传递返回的数据,然后根据您的喜好解析数据。所以在你上面的例子中,你会做这样的事情:

$('#autocomplete').typeahead({                                
name: 'organizations',
prefetch:
{
url: 'jsoncall',
filter: function(data){
// filter the returned data
return [data.movies[0].title, data.movies[1].title, data.movies[2].title];
}
}
}
);

如果您返回的数据集是一个看起来像这样的 JSON 对象,上面的示例将起作用:

movies: [{id:12865, title:Kill Bill: Volume 1, year:2003, mpaa_rating:R, runtime:111,…},…]
0: {id:12865, title:Kill Bill: Volume 1, year:2003, mpaa_rating:R, runtime:111,…}
1: {id:12862, title:Kill Bill, Volume 2, year:2004, mpaa_rating:R, runtime:137,…}
2: {id:771237417, title:Kill Bill: The Whole Bloody Affair, year:2011, mpaa_rating:Unrated, runtime:,…}
3: {id:770998578, title:Angel of Death: Killer Nurse: A Bill Kurtis Special Report, year:2006,…}
4: {id:771352617, title:Kedi Billa Killadi Ranga, year:2013, mpaa_rating:Unrated, runtime:145,…}
total: 5

关于javascript - 在 typeahead.js 中使用预取过滤器(使用基本选项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com