Ajax call populate Typeahead Bootstrap

后端 未结 3 498
慢半拍i
慢半拍i 2020-12-28 18:02

What I\'m trying to do is to get a json object via Ajax and populate the Bootstrap Typeahead with just one kind of value.

Here is my code:

nameTypeHe         


        
相关标签:
3条回答
  • 2020-12-28 18:49

    Checkout this gist. Does autocomplete, deals with fast typers, and caches

    https://gist.github.com/mrgcohen/5062352

    0 讨论(0)
  • 2020-12-28 18:50

    I made it from scratch:

    $('#typeahead').typeahead({
    
        source: function (query, process) {
            return $.getJSON(
                'path/to/lookup',
                { query: query },
                function (data) {
                    return process(data);
                });
        }
    
    });
    

    Where data is a simple JSON array like:

     [
       "John",
       "Jane",
       "Alfredo",
       "Giovanni",
       "Superman"
     ]
    

    If your data array has got a different structure, just rearrange it before passing it to process() method.

    You can find a live example here.

    EDIT - based on your json data:

    [
        {'id':'0', 'name':'John'},
        {'id':'1', 'name':'Jane'}, 
        {'id':'2', 'name':'Alfredo'},
        ...
    }
    

    The getJSON callback becomes:

    function (data) {
    
        var newData = [];
    
        $.each(data, function(){
    
            newData.push(this.name);
    
        });
    
        return process(newData);
    
    }); 
    
    0 讨论(0)
  • 2020-12-28 19:02

    This is what worked for me:-

    HTML Setup :

    <!-- HTML Setup-->
    <input type="text" id="my-typeahead" />
    

    Javascript :

    /* 
    example remote-data-source
    [
      {
        id:1,
        name:'Batman'
      },{
        id:2,
        name:'Superman'
      } 
    ]
    */
    
    $('#my-typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    }, {
        name: 'myDataset',
        source: function(str, sync, async) {
            // abstracting out the "REST call and processing" in a seperate function
            restCall(str, async);
        },
        templates: {
            suggestion: function(user) {
                return '<div>' + user.name + '</div>';
            },
            pending: '<div>Please wait...</div>'
        }
    });
    
    // The function that will make the REST call and return the data
    function restCall(str, async) {
        return $.ajax('http://url-for-remote-data-source/query?name=' + str, {
            // headers and other setting you wish to set-up
            headers: {
                'Content-Type': 'application/json',
            },
            // response
            success: function(res) {
                //processing data on response with 'async'
                return async(res.data);
            }
        });
    }
    

    References:

    Typeahead Docs - Datasets : https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#datasets

    Jquery **$.ajax()** : https://api.jquery.com/jquery.ajax/

    Good Luck.

    0 讨论(0)
提交回复
热议问题