| 1 |
<!doctype html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 |
<title>jQuery UI Autocomplete - XML data parsed once</title> |
| 7 |
<link rel="stylesheet" href="../../themes/base/all.css"> |
| 8 |
<link rel="stylesheet" href="../demos.css"> |
| 9 |
<style> |
| 10 |
.ui-autocomplete-loading { |
| 11 |
background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat; |
| 12 |
} |
| 13 |
</style> |
| 14 |
<script src="../../external/requirejs/require.js"></script> |
| 15 |
<script src="../bootstrap.js"> |
| 16 |
function log( message ) { |
| 17 |
$( "<div/>" ).text( message ).prependTo( "#log" ); |
| 18 |
$( "#log" ).attr( "scrollTop", 0 ); |
| 19 |
} |
| 20 |
|
| 21 |
$.ajax({ |
| 22 |
url: "london.xml", |
| 23 |
dataType: "xml", |
| 24 |
success: function( xmlResponse ) { |
| 25 |
var data = $( "geoname", xmlResponse ).map(function() { |
| 26 |
return { |
| 27 |
value: $( "name", this ).text() + ", " + |
| 28 |
( String.prototype.trim.call( $( "countryName", this ).text() ) || |
| 29 |
"(unknown country)" ), |
| 30 |
id: $( "geonameId", this ).text() |
| 31 |
}; |
| 32 |
}).get(); |
| 33 |
$( "#birds" ).autocomplete({ |
| 34 |
source: data, |
| 35 |
minLength: 0, |
| 36 |
select: function( event, ui ) { |
| 37 |
log( ui.item ? |
| 38 |
"Selected: " + ui.item.value + ", geonameId: " + ui.item.id : |
| 39 |
"Nothing selected, input was " + this.value ); |
| 40 |
} |
| 41 |
}); |
| 42 |
} |
| 43 |
}); |
| 44 |
</script> |
| 45 |
</head> |
| 46 |
<body> |
| 47 |
|
| 48 |
<div class="ui-widget"> |
| 49 |
<label for="birds">London matches: </label> |
| 50 |
<input id="birds" /> |
| 51 |
</div> |
| 52 |
|
| 53 |
<div class="ui-widget" style="margin-top:2em; font-family:Arial"> |
| 54 |
Result: |
| 55 |
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div> |
| 56 |
</div> |
| 57 |
|
| 58 |
<div class="demo-description"> |
| 59 |
<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> |
| 60 |
<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> |
| 61 |
</div> |
| 62 |
</body> |
| 63 |
</html> |