Hi I am using typeahead in twitter bootstrap. What I found here is that in autocomplete dropdown, it selects first option by default. My requirement is that initially it should have nothing selected, it goes to first option only when I press navigation key (up or down) or when I hover over it. Is it possibe using typeahead.
There is the more powerfull typeahead project from Twitter which doesn't have this problem. I don't understand why this project is not included directly in Twitter Bootstrap.
You can see here some examples.
To avoid selection by typeahead first option by default, I used parameter documented in official docs: autoSelect. By default it set to 'true'
My code:
$('#searchString', this.el).typeahead({
source: function (query, process) {
var q = '/autocompleteSearch?searchString=' + query;
return $.get(q, function (data) {
return process(data);
});
},
minLength: 3,
autoSelect: false
});
There is the more powerfull typeahead project from Twitter which doesn't have this problem. I don't understand why this project is not included directly in Twitter Bootstrap.
You can see here some examples.
You can simply override the bootstrap typeahead render method to achieve this. The line items.first().addClass('active')
from the original typeahead plugin will be removed in this override.
$.fn.typeahead.Constructor.prototype.render = function(items) {
var that = this
items = $(items).map(function (i, item) {
i = $(that.options.item).attr('data-value', item)
i.find('a').html(that.highlighter(item))
return i[0]
})
this.$menu.html(items)
return this
};
Although not exactly what you ask, there is a nice workaround here. Basically, you make the text that is inserted by the user into the first typeahead suggestion. As the linked answer mentiones, this is the same behaviour you get from the Chrome address bar.
1-Open bootstrap-typeahead.js and comment items.first().addClass('active')
render: function (items) {
var that = this
items = $(items).map(function (i, item) {
i = $(that.options.item).attr('data-value', item)
i.find('a').html(that.highlighter(item))
return i[0]
})
// items.first().addClass('active')
this.$menu.html(items)
return this
}
I've found a solution here that works by adding this first:
var newRender = function(items) {
var that = this
items = $(items).map(function (i, item) {
i = $(that.options.item).attr('data-value', item)
i.find('a').html(that.highlighter(item))
return i[0]
})
this.$menu.html(items)
return this
};
$.fn.typeahead.Constructor.prototype.render = newRender;
$.fn.typeahead.Constructor.prototype.select = function() {
var val = this.$menu.find('.active').attr('data-value');
if (val) {
this.$element
.val(this.updater(val))
.change();
}
return this.hide()
};
which overrides the bootstrap typeahead to not initially select anything
By default the "typeahead-focus-first" is set to true, just set it to false by typeahead-focus-first="false".
© 2022 - 2024 — McMap. All rights reserved.