【问题标题】:Map typeahead suggestions after they are fetched提取后映射预先输入的建议
【发布时间】:2014-02-25 12:23:17
【问题描述】:

我正在使用最后一个 twitter typeahead。

var suggestions = new Bloodhound({
        datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.value); },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {url:'/xxx?text=%QUERY',
            ajax:{
                type : 'post',
                dataType : 'json'
            }
        }
    });
    suggestions.initialize();

    $('.typeahead').typeahead(null, {
        displayKey: 'id',
        source: suggestions.ttAdapter(),
        templates: {
            empty: '<p style="width:50%;margin:auto;">No result</p>',
            suggestion: function(object){
                    var str = ['<p>',
                        object.id,
                        '</p><a class="btn btn-info btn-small" href="item.html?id="',
                        object.id,
                        '>Open</a>'].join('');

                    return str;
                }
        }
    });

问题是我收到了 5 个类型的对象

{
id: 0,
field: '', 
value: ''
}

所有 5 个对象都有相同的 id,因此是一个结果的字段,应该放入 一个建议。 Typeahead 将它们视为单独的建议我不想要

我如何映射 ajax 调用的结果,对其进行转换并将结果传回 typeahead?

【问题讨论】:

    标签: javascript typeahead.js typeahead twitter-typeahead bloodhound


    【解决方案1】:

    您可以使用filter(parsedResponse) 自己解析响应。你可以看到一个例子(一个不相关的答案)here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多