2014-10-18 67 views
4

我正在嘗試使用typeahead來顯示Google建議。Typeahead顯示結果爲undefined

AJAX調用工作正常,正常返回的數據:

之前執行恢復處理(數據); 數據包含以「w」開頭的字符串數組。

數據= 「沃爾瑪」, 「天氣」, 「富國銀行」, 「worldstarhiphop」, 「沃爾格林」, 「維基百科」, 「白頁」, 「世界盃」, 「WebMD的」, 「天氣雷達」]

但是,顯示的建議顯示「未定義」,而不是真正的單詞。 有什麼想法我在這裏失蹤?謝謝。

enter image description here

<input type="text" class="typeahead" placeholder="Search"> 


    $('.typeahead').typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1 
    }, 
    { 
     source: function (query, process) { 
      $.getJSON("Home/Suggest", { query: query }, function (data) { 
       return process(data); 
      }); 
     } 
    }); 

回答

7

更新:

經過一番研究,我發現了一個回答我的問題,並會張貼在這裏,如果有人需要它。

訣竅是 - 「進程」 回調函數期望的結果的格式:

[{值: 「字符串1」},{值: 「字符串2」},{值: 「STRING3」}]

而不僅僅是一串字符串。

$('.typeahead').typeahead(
{ hint: true, highlight: true, minLength: 1 }, // options 
{ 
    source: function (query, process) { // source dataset, data = array of strings 
     $.getJSON('Home/Suggest', { query: query }, function (data) { 
      //data=["string1", "string2", "string3"] 
      //process callback function needs it 
      //in a format [{value: "string1"}, {value: "string2"}, {value: "string3"}] 
      var output = $.map(data, function (string) { return { value: string }; }); 
      process(output); 
     }); 
    } 
}); 
+0

謝謝。鍵入文檔中也有舊版本。 – user257980 2015-11-22 12:52:08

+0

我得到了同樣的錯誤,我正在關注[this](http://twitter.github.io/typeahead.js/examples/#the-basics)示例。我的來源是substringMatcher(狀態)和狀態是一個數組。 – user4584963 2015-12-28 03:10:35

+0

https://gist.github.com/jharding/9458744#file-the-basics-html修復了破解的初始示例http://twitter.github.io/typeahead.js/examples/ – 2016-11-22 14:21:41