Using a prefetch filter using typeahead.js (using basic options)

I am using typeahead.js . I use prefetch options and must parse the returned data. It's not a mistake; he does not do anything. I searched for examples, but none of them used the Prefetch Filter parameter.

Link to prefetch documentation

My code (which does not work but does not cause errors):

$('#autocomplete').typeahead('destroy').typeahead( { name: 'organizations', prefetch: { url: 'jsoncall', filter: function() { // Blatant hardcoded return value return ['test-a','test-b','test'c]; } } } ); 

My HTML:

 <input id="autocomplete" class="large-12" autocomplete="off" type="text" placeholder="Enter school name"> 

My confusion:

 0________0 
+4
source share
1 answer

You can pass the returned data through the filter function, which then analyzes the data to your liking. So, in your example above, you would do something like this:

 $('#autocomplete').typeahead({ name: 'organizations', prefetch: { url: 'jsoncall', filter: function(data){ // filter the returned data return [data.movies[0].title, data.movies[1].title, data.movies[2].title]; } } } ); 

The example above will work if your returned dataset was a JSON object that looked something like this:

 movies: [{id:12865, title:Kill Bill: Volume 1, year:2003, mpaa_rating:R, runtime:111,…},…] 0: {id:12865, title:Kill Bill: Volume 1, year:2003, mpaa_rating:R, runtime:111,…} 1: {id:12862, title:Kill Bill, Volume 2, year:2004, mpaa_rating:R, runtime:137,…} 2: {id:771237417, title:Kill Bill: The Whole Bloody Affair, year:2011, mpaa_rating:Unrated, runtime:,…} 3: {id:770998578, title:Angel of Death: Killer Nurse: A Bill Kurtis Special Report, year:2006,…} 4: {id:771352617, title:Kedi Billa Killadi Ranga, year:2013, mpaa_rating:Unrated, runtime:145,…} total: 5 
+6
source

Source: https://habr.com/ru/post/1480998/


All Articles