【问题标题】:Typeahead: Adding a last optionTypeahead:添加最后一个选项
【发布时间】:2014-03-02 01:13:09
【问题描述】:

我正在使用 Bootstrap v2.3.2,并且我有一个常规的预先输入设置,可以从数据库中获取数据。

我正在努力使每个预先输入的最后一个结果是选项“搜索”。因此,如果您在输入中键入“hello”,最后一个选项(在所有其他结果下)将是 Search for "hello"

如何做到这一点?

【问题讨论】:

  • 你能分享你的预输入代码吗?

标签: javascript jquery twitter-bootstrap bootstrap-typeahead


【解决方案1】:

需要修改Typeahead的source参数。

这是我在我的网站中实现相同功能的方式:-

    $("#typeahead").typeahead({
        'source'=>function(query, process){
            return $.get("URL_OF_SEARCH", {string:query}, function(data){
                data = JSON.parse(data); 
                var list = $.map(data, function(item){
                     var aItem = {
                         id: item.id, name: item.text, picture: item.picture
                     }; 
                     return JSON.stringify(aItem);
                }); 
                //Till here everything was done to fetch previous results and show them
                list.push({"id":"URL_OF_SEARCH/" + query ,"name":"Search for "+query, "picture":"/favicon.ico"}); 
                //This above line adds the last row which you want to add
                return process(list);
             });
        },
        'highlighter'=>function(item){
            item = JSON.parse(item);
            var itm = "<div class='typeahead_wrapper'>"
            + "<img class='typeahead_photo' src='" + item.picture + "'/>"
            + "<div class='typeahead_labels'>"
            + "<div class='typeahead_primary'>" + item.name + "</div></div></div>";
            return itm;
        },
     });

我在列表中使用了 3 个东西来显示,一张图片、一个名称和一个链接(ID)。为了实现这一点,我还必须修改我的荧光笔功能。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多