Pregunta

I am using the last 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;
                }
        }
    });

The problem is that I recieve 5 objects of type

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

All 5 objects have the same id and therefore are fields of one result and should be put into one suggestion. Typeahead treats them as separate suggestions which I don't want.

How do I map over the results of the ajax call, transform it and pass the result back to typeahead?

¿Fue útil?

Solución

You could use the filter(parsedResponse) to do the parsing of the response yourself. You can see an example (to an unrelated answer) here.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top