views:

42

answers:

3

Hi.

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?

Thanks.

A: 

Is it even possible to do this fom jquery side? Might be you have to load and join the files "by hand".

joni
I am already doing it with 1 xml file why wouldn't I be able to make a second request? I know I can just request all the documents and then parse them together then pass that to jquery but the extra step seams like a waste of time. I'd rather just make 2 separate source requests.
specked
Might be jQuery _has_ the functionality implemented to use 2 files as source, or might be _not_. As far as I know, it doesn't have this functionality, so you have to do it by yourself. Another approach would be on the server side, can't you just join the files there?
joni
A: 

First of all, the docs say that "the Autocomplete plugin expects that string to point to a URL resource that will return JSON data." Note: JSON, not XML so you need to convert your xml to json in the following.

The xml to json can be done on your server or on the client browsers. It will be faster to do it once on your server, if possible.

To use multiple source files, you will need to first load the multiple files in your HTML/JS page. Then concatenate them together into a Javascript array, then provide the array to the autocomplete call.

Something like:

<script>
  myproject_choices = []; // global var. Hence prefix with project name.

  var cb = function(data){jQuery.merge(myproject_choices, data);}; // callback for ajax

  $.getJSON('ajax/choices1.json', cb); // fetch and concatenate the choices
  $.getJSON('ajax/choices2.json', cb);
  $.getJSON('ajax/choices3.json', cb);
</script>

# later...
$( ".selector" ).autocomplete({source: myproject_choices });
Larry K
A: 

I might be possible by writing a custom source function:

$("#elm").autocomplete({ source: function(request, response) {
$.ajax({
   url: "links1.xml",
   success: function(data)
   {
           // store data here
           $.ajax({
               url: "links2.xml",
               success: function(data)
               {  
                  // combine data from first call with this call
                  response(combineddata);
               }
           }
   });
}....

but i've you're able to combine the files at some other point that might be preferable.

Xml with autocomplete: http://jqueryui.com/demos/autocomplete/#xml

Milo
@Larry K: xml can be parsed to an array of objects which can be used for "respone()"Each object shoud have an id, value, label and both attribute.
Milo
@Larry K:You're solution will work but might not be a working solution if the sources aren't static.
Milo
@Milo: The OP was using a static source via a url.
Larry K
@Larry K: indeed, so if he has the option to convert it to json on the server your solution would be preferable and definitly faster.
Milo
I have no server-side abilities. Everything is being driven client side via javascript
specked