Typeahead.js: Append (not replace) selected suggestion to input

Created on 20 Aug 2015  路  1Comment  路  Source: twitter/typeahead.js

No events or hooks or option allows selected suggestion to be appended to the current input, instead of being replaced.

Most helpful comment

You can implement your own workaround with the existing typeahead functionality if you use the displayKey and templates properties in your dataset. 'displayKey' will dictate what actual text gets set in the input widget after you select a suggestion. The 'templates' property lets you set a suggestion template function to control what gets displayed.

For example:

        typeahead.typeahead({
            highlight: true,
        }, {
            name: 'example',
            displayKey: 'output',
            source: suggester,
            templates: {
                suggestion: function (i) {
                    return '<p>' + i.value + '</p>'
                }
            }
        });

suggester is your source callback that should generate suggestions with both an output field and a value field. You'd set output to be the concatenated string of the previous value (the query argument passed to the source callback) plus the suggestion. Your value field would just be the suggestion for abbreviated display. For example:

        var suggester = function(query, syncResults) {
            var results = {value: i, query: query + i};
            syncResults(results);
        }

If you'd like to do a partial term replacement rather than just a straight up append, you'll need to do some combination of tokenization/parsing/string manipulation to manage replacing just that trailing partial word.

>All comments

You can implement your own workaround with the existing typeahead functionality if you use the displayKey and templates properties in your dataset. 'displayKey' will dictate what actual text gets set in the input widget after you select a suggestion. The 'templates' property lets you set a suggestion template function to control what gets displayed.

For example:

        typeahead.typeahead({
            highlight: true,
        }, {
            name: 'example',
            displayKey: 'output',
            source: suggester,
            templates: {
                suggestion: function (i) {
                    return '<p>' + i.value + '</p>'
                }
            }
        });

suggester is your source callback that should generate suggestions with both an output field and a value field. You'd set output to be the concatenated string of the previous value (the query argument passed to the source callback) plus the suggestion. Your value field would just be the suggestion for abbreviated display. For example:

        var suggester = function(query, syncResults) {
            var results = {value: i, query: query + i};
            syncResults(results);
        }

If you'd like to do a partial term replacement rather than just a straight up append, you'll need to do some combination of tokenization/parsing/string manipulation to manage replacing just that trailing partial word.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamasantares picture adamasantares  路  5Comments

a-lucas picture a-lucas  路  5Comments

Neeeeena picture Neeeeena  路  3Comments

palintropos picture palintropos  路  4Comments

digitalpenguin picture digitalpenguin  路  4Comments