簡體   English   中英

帶有遠程數組的Twitter typeahead.js

[英]Twitter typeahead.js with remote array

我想使用twtter的提前輸入來顯示一些輸入建議。 這些建議將來自服務器,因此我需要使用遠程表單: http : //twitter.github.io/typeahead.js/examples/

但是遠程示例都返回json。 由於我將擁有大量數據,並且我想將其存儲在Redis中,所以我認為我不會將數據存儲在json中,而是存儲在值數組中。 節省內存。

我還沒有參與Redis的工作,只是嘗試返回一個數組。 但是我只是得到了很多未定義的選項可供選擇。

任何指示將不勝感激。

我在Rails上。 假設我將從服務器返回render:text => [“” abc“,” sdf“,” erw“,gfg”],我該如何預先輸入以基於該響應顯示建議?

這是我想適應采用數組而不是json的示例:

var bestPictures = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../data/films/post_1960.json',
  remote: '../data/films/queries/%QUERY.json'
});

bestPictures.initialize();

$('#remote .typeahead').typeahead(null, {
  name: 'best-pictures',
  displayKey: 'value',
  source: bestPictures.ttAdapter()
});

謝謝。

我會自己回答

<script type="text/javascript">

$(function() {
      var xx = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace("user"),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      remote: {
        url: '/home/cp?c=%QUERY',
         filter: function(list) {
            console.log(list)
            return $.map(list, function(name) { return { user: name}; });
        }
      } 
    });

    xx.initialize();

    $('#remote .typeahead').typeahead(null, {
      name: 'xx',
      displayKey: 'user',
      source: xx.ttAdapter()
    }); 


})


</script>



<div id="remote" style="text-align:center; margin-top: 200px;">
    <input class="typeahead" type="text">
</div>

因此,從控制器返回render json:%w [foo bar]將顯示可用選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM