数组到其他数组,自动完成

时间:2017-01-17 15:26:52

标签: javascript arrays ajax

我如何能够成为这个函数的好成员:

window.onload = function(){
    var availableTags=getAllUserIdName();
    $( "#members" ).autocomplete({
        source: availableTags 
    });
};

function getAllUserIdName(){
    $.get('{{url('/')}}/ajax-all-user-id-name',function(data){
        return data;
    });
}

我收到了回复:

[{"name":"asd"},{"name":"qwqe"},{"name":"gipsz jakab"}]

THX!

1 个答案:

答案 0 :(得分:0)

var testData = [
  {"name":"asd"},{"name":"qwqe"},{"name":"gipsz jakab"}
];

getAllUserIdName(function(tags){
  $("#members").autocomplete({ source: tags });
});
    
function getAllUserIdName(callback){
    // simulate asynchronous request for data:
    setTimeout(function(){
        var data = testData;
        var tags = data.map(function(item){ return item.name; });
        callback(tags);
    }, 300);
      
    /*$.get('{{url('/')}}/ajax-all-user-id-name',function(data){
        var tags = data.map(function(item) { return item.name; }); 
        callback(tags);
    });*/
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <label for="members">Members: </label>
  <input id="members">