Typeahead and Bloodhound show unrelated sentences when using 'remote'

When using Typeahead / Bloodhound with the remote option, when the local / prefetch results are under the “limit” (5), the above sentences are not related to input. He seems to like to show only the top of the results given up to 5.

Photo: "Love" - ​​the expected result, everything else is not connected:

My code is:

    var keywords = [
 {"value": "Ambient"}, {"value": "Blues"},{"value":  "Cinematic"},{"value":  "Classical"},{"value": "Country"},
 {"value": "Electronic"},{"value": "Holiday"},{"value": "Jazz"},{"value": "Lounge"},{"value": "Folk"},
  {"value": "Hip Hop"},{"value": "Indie"},{"value": "Pop"},{"value": "Post Rock"},{"value": "Rock"},{"value": "Singer-Songwriter"},{"value": "Soul"},
  {"value": "World"},{"value": "Happy"},{"value": "Sad"},{"value": "Love"},{"value": "Angry"},
  {"value":"Joy"},{"value": "Delight"},{"value": "Light"},{"value": "Dark"},{"value": "Religious"},{"value": "Driving"},
  {"value":"Excited"},{"value": "Yummy"},{"value": "Delicious"},{"value": "Fun"},{"value": "Rage"},
  {"value":"Hard"},{"value": "Soft"}
  ];


// Instantiate the Bloodhound suggestion engine
var keywordsEngine = new Bloodhound({
    datumTokenizer: function (datum) {
        return Bloodhound.tokenizers.whitespace(datum.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: keywords,
    remote: {
        url: '/stub/keywords.json',
        filter: function (keywords) {
            // Map the remote source JSON array to a JavaScript object array
            return $.map(keywords, function (keyword) {
                return {
                    value: keyword.value
                };
            });
        }
    },
    prefetch: {
        url: '/stub/keywords.json',
        filter: function (keywords) {
            // Map the remote source JSON array to a JavaScript object array
            return $.map(keywords, function (keyword) {
                return {
                    value: keyword.value
                };
            });
        }
    }
});

// kicks off the loading/processing of `local` and `prefetch`
keywordsEngine.initialize();

$('#keyword-search-input').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'keyword',
  displayKey: 'value',
  // `ttAdapter` wraps the suggestion engine in an adapter that
  // is compatible with the typeahead jQuery plugin
  source: keywordsEngine.ttAdapter()
});
+4
source share
2 answers

For further research, I think I need to filter out the deleted sentences manually, according to this thread on Github questions for Typeahead.js:

", , , , , , ".

https://github.com/twitter/typeahead.js/issues/148

+2

. , JavaScript . Bloodhound remote url. - , - json, typeahead limit. , limit: 10, 10 json, , , . json , .

remote query (, ), , , .

, , json, - , prefecth? , remote. PHP , - :

$key=$_GET['key'];
$con=mysqli_connect("localhost","root","");
$db=mysqli_select_db($con, "database_name");
$query=mysqli_query($con, "SELECT * FROM table WHERE column LIKE '%{$key}%'");
$rows=array();
while($row=mysqli_fetch_assoc($query))
{
  $rows[] = $row; 
}
echo json_encode($rows);        

GET, , " " .

0

All Articles