Jquery ui autocomplete - multiple sources

前端 未结 4 1420
说谎
说谎 2021-01-16 08:24

For 1 source this is the correct code after the ajax call: url: \"links2.xml\",

I would like the source to be multiple xml files. How do I include the extra paths

4条回答
  •  有刺的猬
    2021-01-16 09:11

    I would separate the task into

    1. The retrieval of data
    2. Populating the autocomplete

    If you're able to load data from multiple sources and uniform them, you can use the result to populate the autocomplete-control. I suggest you look into loading data async using jQuery Deferred-objects (api.jquery.com/jQuery.Deferred) and wait for all calls to return and use the result using $.when(...).then(...)

    Example below originates from the well-written and quite well explained site: http://www.danieldemmel.me/blog/2013/03/22/an-introduction-to-jquery-deferred-slash-promise/

    function getReady() {
      var deferredReady = $.Deferred();
      $(document).ready(function() {
        deferredReady.resolve();
      });
      return deferredReady.promise();
    }
    
    var firstRequest = $.ajax({ url: 'http://www.html5rocks.com/en/tutorials/file/xhr2/' });
    var secondRequest = $.ajax({ url: 'http://www.html5rocks.com/en/tutorials/audio/scheduling/' });
    
    $.when( getReady(), firstRequest, secondRequest
    ).done( function( readyResponse, firstResponse, secondResponse ) {
      var insertDiv1 = $('
    '); insertDiv1.html($(firstResponse[0]).find('section').html()); var insertDiv2 = $('
    '); insertDiv2.html($(secondResponse[0]).find('section').html()); $('body').append(insertDiv1, '
    ', insertDiv2); });

提交回复
热议问题