Bootstrap Typeahead: Remove forced selection of first item
Asked Answered
C

7

7

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.

Cyclosis answered 26/12, 2012 at 0:29 Comment(7)
Have you tried using the highlighter option with a value of -1?Tresa
no I will try that, let me check.Cyclosis
@Tresa sorry it is not working, and highlighter is a function, how it can take value.Cyclosis
You can pass it as a data attribute data-highlighter="-1"Tresa
@Tresa it is not working now also. I guess passing it through data attribute will not do the job, since I am doing everything through javascript.Cyclosis
any luck yet? i am also looking for the solutionHamal
@Hamal No, I have switched to jquery autocomplete. It is better than typeahead, for this case.Cyclosis
K
2

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.

Kennakennan answered 6/5, 2013 at 12:55 Comment(1)
typeahead.js.org/examples is the newer / maintained code base forked off the twitter typeahead that has not been maintained in years.Gunlock
I
7

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
    });
Indiscrete answered 27/8, 2015 at 18:7 Comment(0)
K
2

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.

Kennakennan answered 6/5, 2013 at 12:55 Comment(1)
typeahead.js.org/examples is the newer / maintained code base forked off the twitter typeahead that has not been maintained in years.Gunlock
M
2

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
};
Metalinguistics answered 27/11, 2013 at 7:3 Comment(0)
G
1

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.

Galvanism answered 18/1, 2013 at 10:21 Comment(2)
Thanks for this suggestion. I agree it doesn't answer the exact question but it ended up being the best solution for meVinic
@DerekWhite How did you fix this problem? Where is there an example? Thanks!Toots
A
0

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
}
Albaugh answered 26/8, 2014 at 9:28 Comment(0)
T
0

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

Tormentor answered 13/6, 2016 at 9:43 Comment(0)
S
0

By default the "typeahead-focus-first" is set to true, just set it to false by typeahead-focus-first="false".

Sarajane answered 18/9, 2018 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.