Home » Javascript » How to set the value of input using typeahead?

How to set the value of input using typeahead?

Posted by: admin November 1, 2017 Leave a comment

Questions:

In this point I can select the text o of a remote data, but a want that when I select an option, set the value of the input to the id of the option, hoy can I do this? Thaks a lot.

var engine = new Bloodhound({
 remote: {
  url: '/find?q=%QUERY%',
  wildcard: '%QUERY%',
 },
 datumTokenizer: Bloodhound.tokenizers.whitespace('dinicial'),
 queryTokenizer: Bloodhound.tokenizers.whitespace
});

$('#dinicial').typeahead({
  hint: true,
  highlight: true,
  minLength: 3
}, {
source: engine.ttAdapter(),

// This will be appended to "tt-dataset-" to form the class name of the 
 suggestion menu.
name: 'diagnosticList',
limit: 10,
displayKey: function (data) {
    return data.nombre;
},

// the key from the array we want to display (name,id,email,etc...)
templates: {
    empty: [
        '<div class="list-group search-results-dropdown"><div class="list-
 group-item">No se hallaron coincidencias.</div></div>'
     ],
    header: [
        '<div class="list-group search-results-dropdown">'
    ],
    suggestion: function (data) {
        return '<label class="list-group-item">' + data.nombre + '</label>'
      }
  },
});`
Answers: