Project

General

Profile

1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
	<meta charset="utf-8">
5
	<title>jQuery UI Autocomplete - XML data parsed once</title>
6
	<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
7
	<script src="../../jquery-1.4.3.js"></script>
8
	<script src="../../ui/jquery.ui.core.js"></script>
9
	<script src="../../ui/jquery.ui.widget.js"></script>
10
	<script src="../../ui/jquery.ui.position.js"></script>
11
	<script src="../../ui/jquery.ui.autocomplete.js"></script>
12
	<link rel="stylesheet" href="../demos.css">
13
	<style>
14
	.ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
15
	</style>
16
	<script>
17
	$(function() {
18
		function log( message ) {
19
			$( "<div/>" ).text( message ).prependTo( "#log" );
20
			$( "#log" ).attr( "scrollTop", 0 );
21
		}
22

    
23
		$.ajax({
24
			url: "london.xml",
25
			dataType: "xml",
26
			success: function( xmlResponse ) {
27
				var data = $( "geoname", xmlResponse ).map(function() {
28
					return {
29
						value: $( "name", this ).text() + ", " +
30
							( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
31
						id: $( "geonameId", this ).text()
32
					};
33
				}).get();
34
				$( "#birds" ).autocomplete({
35
					source: data,
36
					minLength: 0,
37
					select: function( event, ui ) {
38
						log( ui.item ?
39
							"Selected: " + ui.item.value + ", geonameId: " + ui.item.id :
40
							"Nothing selected, input was " + this.value );
41
					}
42
				});
43
			}
44
		});
45
	});
46
	</script>
47
</head>
48
<body>
49

    
50
<div class="demo">
51

    
52
<div class="ui-widget">
53
	<label for="birds">London matches: </label>
54
	<input id="birds" />
55
</div>
56

    
57
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
58
	Result:
59
	<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
60
</div>
61

    
62
</div><!-- End demo -->
63

    
64

    
65

    
66
<div class="demo-description">
67
<p>This demo shows how to retrieve some XML data, parse it using jQuery's methods, then provide it to the autocomplete as the datasource.</p>
68
<p>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.</p>
69
</div><!-- End demo-description -->
70

    
71
</body>
72
</html>
(15-15/15)