Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. jQuery(function ($) {
  2. $('.airport-autocomplete').each(function () {
  3. var apca = new apc('autocomplete', {
  4. key : 'xxxxxxxxxx',
  5. //secret : 'xxxxxxxxxxxxxxx', // Your API Secret Key: use this if you are not connecting from a web server
  6. limit : 7
  7. });
  8.  
  9. var dataObj = {
  10. source: function( request, response ) {
  11. // make the request
  12. apca.request( request.term );
  13.  
  14. // this builds each line of the autocomplete
  15. itemObj = function (airport, isChild) {
  16. var label;
  17. if (isChild) { // format children labels
  18. label = '↳' + airport.iata + ' - ' + airport.name;
  19. } else { // format labels
  20. label = airport.city;
  21. if (airport.state.abbr) {
  22. label += ', ' + airport.state.abbr;
  23. }
  24. label += ', ' + airport.country.name;
  25. label += ' (' + airport.iata + ' - ' + airport.name + ')';
  26. }
  27. return {
  28. label: label,
  29. value: airport.iata + ' - ' + airport.name,
  30. code: airport.iata
  31. };
  32. };
  33.  
  34. // this deals with the successful response data
  35. apca.onSuccess = function (data) {
  36. var listAry = [],
  37. thisAirport;
  38. if (data.status) { // success
  39. for (var i = 0, len = data.airports.length; i < len; i++) {
  40. thisAirport = data.airports[i];
  41. listAry.push(itemObj(thisAirport));
  42. if (thisAirport.children) {
  43. for (var j = 0, jLen = thisAirport.children.length; j < jLen; j++) {
  44. listAry.push(itemObj(thisAirport.children[j], true));
  45. }
  46. }
  47. }
  48. response(listAry);
  49. } else { // no results
  50. response();
  51. }
  52. };
  53. apca.onError = function (data) {
  54. response();
  55. console.log(data.message);
  56. };
  57. },
  58. select: function( event, ui ) {
  59. // do something for click event
  60. console.log(ui.item.code);
  61. }
  62. };
  63.  
  64. // this is necessary to allow html entities to display properly in the jqueryUI labels
  65. $(this).autocomplete(dataObj).data("ui-autocomplete")._renderItem = function( ul, item) {
  66. return $('<li></li>').data('item.autocomplete', item ).html( item.label ).appendTo( ul );
  67. };
  68. });
  69. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement