1
|
1 |
<!DOCTYPE html> |
|
2 |
<html lang="en"> |
|
3 |
<head> |
|
4 |
<meta charset="UTF-8" /> |
|
5 |
<title>jQuery UI Autocomplete Remote datasource demo</title> |
|
6 |
<link type="text/css" href="../../themes/base/jquery.ui.all.css" rel="stylesheet" /> |
|
7 |
<script type="text/javascript" src="../../jquery-1.4.2.js"></script> |
|
8 |
<script type="text/javascript" src="../../ui/jquery.ui.core.js"></script> |
|
9 |
<script type="text/javascript" src="../../ui/jquery.ui.widget.js"></script> |
|
10 |
<script type="text/javascript" src="../../ui/jquery.ui.position.js"></script> |
|
11 |
<script type="text/javascript" src="../../ui/jquery.ui.autocomplete.js"></script> |
|
12 |
<link type="text/css" href="../demos.css" rel="stylesheet" /> |
|
13 |
<script type="text/javascript"> |
|
14 |
$(function() { |
|
15 |
function log(message) { |
|
16 |
$("<div/>").text(message).prependTo("#log"); |
|
17 |
$("#log").attr("scrollTop", 0); |
|
18 |
} |
|
19 |
|
|
20 |
$.ajax({ |
|
21 |
url: "london.xml", |
|
22 |
dataType: "xml", |
|
23 |
success: function(xmlResponse) { |
|
24 |
var data = $("geoname", xmlResponse).map(function() { |
|
25 |
return { |
|
26 |
value: $("name", this).text() + ", " + ($.trim($("countryName", this).text()) || "(unknown country)"), |
|
27 |
id: $("geonameId", this).text() |
|
28 |
}; |
|
29 |
}).get(); |
|
30 |
$("#birds").autocomplete({ |
|
31 |
source: data, |
|
32 |
minLength: 0, |
|
33 |
select: function(event, ui) { |
|
34 |
log(ui.item ? ("Selected: " + ui.item.value + ", geonameId: " + ui.item.id) : "Nothing selected, input was " + this.value); |
|
35 |
} |
|
36 |
}); |
|
37 |
} |
|
38 |
}) |
|
39 |
|
|
40 |
}); |
|
41 |
</script> |
|
42 |
</head> |
|
43 |
<body> |
|
44 |
|
|
45 |
<div class="demo"> |
|
46 |
|
|
47 |
<div class="ui-widget"> |
|
48 |
<label for="birds">London matches: </label> |
|
49 |
<input id="birds" /> |
|
50 |
</div> |
|
51 |
|
|
52 |
<div class="ui-widget" style="margin-top:2em; font-family:Arial"> |
|
53 |
Result: |
|
54 |
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div> |
|
55 |
</div> |
|
56 |
|
|
57 |
</div><!-- End demo --> |
|
58 |
|
|
59 |
<div class="demo-description"> |
|
60 |
<p> |
|
61 |
This demo shows how to retrieve some XML data, parse it using jQuery's methods, then provide it to the autocomplete as the datasource. |
|
62 |
</p> |
|
63 |
<p> |
|
64 |
This should also serve as a reference on how to parse a remote XML datasource - the parsing would just happen for each request within the source-callback. |
|
65 |
</p> |
|
66 |
</div><!-- End demo-description --> |
|
67 |
|
|
68 |
</body> |
|
69 |
</html> |