预先建议列表不减少

问题描述 投票:0回答:1

我正在尝试植入twitter typeahead.js

https://github.com/corejavascript/typeahead.js

但是建议列表不会减少。...

HTML代码:

<div class="container">
    <form action="" method="POST">
        <div class="typeahead-wrapper">
            <input type="text" id="my_search" name="search" autocomplete="off" placeholder="Enter Country Code"/>
            <input type="submit">
        </div>
    </form>
</div>

Javascript代码

$(document).ready(function($) {
  var cities_suggestions = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace("DESIGNATION"),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
      url: "villes2.json",
      transform: function(data) {
        var newData = [];
        data.forEach(function(item) {
          newData.push(item.DESIGNATION);
        });
        console.log(newData);
        return newData;
      }
    }
  });

  cities_suggestions.initialize();

  $("#my_search").typeahead(
    {
      hint: true,
      minLength: 1,
      highlight: true
    },
    {
      name: "cities",
      source: cities_suggestions, // suggestion engine is passed as the source
      limit: 10
    }
  );
});

我的Json文件如下所示:

[
  {
    "CODE_INSEE": 1344,
    "DESIGNATION": "1000 SAINT-DENIS-LES-BOURG"
  },
  {
    "CODE_INSEE": 1053,
    "DESIGNATION": "1000 BOURG-EN-BRESSE"
  },
  {
    "CODE_INSEE": 1225,
    "DESIGNATION": "1090 LURCY"
  },
  {
    "CODE_INSEE": 1165,
    "DESIGNATION": "1090 FRANCHELEINS"
  },
  {
    "CODE_INSEE": 1263,
    "DESIGNATION": "1090 MONTMERLE-SUR-SAONE"
  },
  {
    "CODE_INSEE": 1169,
    "DESIGNATION": "1090 GENOUILLEUX"
  }
]

我仍然有json文件的十个优先建议以及其他任何东西console.log(NewData)可以]

typeahead.js bloodhound
1个回答
0
投票

我发现

$(document).ready(function($) {
var cities_suggestions = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: 'villes2.json',
    remote: {
        url: 'villes2/queries/%QUERY.json',
        wildcard: '%QUERY'
  },
});
cities_suggestions.initialize();
// init Typeahead
$('#my_search').typeahead(
{
    hint: true,
    minLength: 1,
    highlight: true
},
{
    name: 'cities',
    display: 'text',
    source: cities_suggestions,   // suggestion engine is passed as the source
    limit: 10,
});
// then fill hidden input for get the value :-)
$('#my_search').bind('typeahead:select', function(ev, suggestion) {
    $('#my_search_value').val(suggestion.value)
});

});

© www.soinside.com 2019 - 2024. All rights reserved.