2012-12-26 27 views
6

嗨,我在twitter引導中使用typeahead。我在這裏找到的是,在自動完成下拉菜單中,它默認選擇第一個選項。我的要求是,最初它應該沒有選擇任何東西,只有當我按導航鍵(向上或向下)或當我將鼠標懸停在上面時,它纔會成爲第一個選項。它是否使用typeahead。Bootstrap Typeahead:刪除第一項的強制選擇

+0

您是否嘗試過使用值爲-1的高亮選項? – Tkingovr

+0

不,我會試試,讓我檢查一下。 –

+0

@Tkingovr對不起,它不起作用,熒光筆是一個功能,它如何能夠帶來價值。 –

回答

2

還有更強大的typeahead project從Twitter沒有這個問題。我不明白爲什麼這個項目不是直接包含在Twitter Bootstrap中。

你可以看到here some examples

+1

https://github.com/twitter/bootstrap/issues/7384這裏是爲什麼 – Jorge

+0

http://angulartutorial.blogspot.in/2014/05/auto-complete-in-angular-js.html – Prashobh

+0

https:// typeahead .js.org/examples /是更新/維護的代碼庫,分離出未曾多年維護的twitter typeahead。 – Artistan

1

雖然不完全是你要求的,但有一個很好的解決方法here。基本上,您可以將用戶插入的文本置入第一個類型提示建議中。正如鏈接的答案所提到的,這與Chrome地址欄中的行爲相同。

+0

是的,我給了它一個想法,但它不正確。 –

+0

感謝您的建議。我同意它不回答確切的問題,但它最終成爲我的最佳解決方案 –

+0

@DerekWhite你是如何解決這個問題的?哪裏有一個例子?謝謝! – hyperrjas

2

您可以簡單地覆蓋引導程序typeahead渲染方法來實現此目的。原先的typeahead插件中的items.first().addClass('active')行將在此重寫中移除。

$.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 
}; 
0

1開自舉-typeahead.js 和評論items.first()。addClass( '激活')

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 
} 
4

爲了避免選擇由預輸入第一個選項默認,我在官方文檔中記錄使用的參數:autoSelect。默認情況下,將其設置爲「真」

我的代碼:

$('#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 
    }); 
0

我已經找到了解決辦法here,通過增加工作的這第一:

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() 
}; 

它覆蓋引導預輸入不最初選擇任何東西

相關問題