admin管理员组

文章数量:1394743

I am using typeahead.js. I am using the prefetch options and need to parse the returned data. It's not throwing an error; it's not doing much of anything. I've looked for examples but none of them use the Prefetch Filter option.

Link to the prefetch documentation

My code (that does not work but throws no errors):

$('#autoplete').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="autoplete" class="large-12" autoplete="off" type="text" placeholder="Enter school name">

My Confusion:

0________0

I am using typeahead.js. I am using the prefetch options and need to parse the returned data. It's not throwing an error; it's not doing much of anything. I've looked for examples but none of them use the Prefetch Filter option.

Link to the prefetch documentation

My code (that does not work but throws no errors):

$('#autoplete').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="autoplete" class="large-12" autoplete="off" type="text" placeholder="Enter school name">

My Confusion:

0________0
Share Improve this question edited Jul 2, 2013 at 23:42 Meredith 4,4845 gold badges36 silver badges58 bronze badges asked May 15, 2013 at 17:48 Jesse BurcsikJesse Burcsik 3673 silver badges13 bronze badges 1
  • This has helped...stackoverflow./questions/16321544/… – Jesse Burcsik Commented May 15, 2013 at 22:45
Add a ment  | 

1 Answer 1

Reset to default 6

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

$('#autoplete').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 would work if your returned data set 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

本文标签: javascriptUsing Prefetch Filter with typeaheadjs (Using basic options)Stack Overflow