Advertisement
Guest User

From

a guest
Aug 28th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var $location = $("#location");
  2.     $location.autocomplete({
  3.         source: function(request, response) {
  4.             $.ajax({
  5.                 url: "//ws.geonames.org/searchJSON",
  6.                 dataType: "jsonp",
  7.                 data: {
  8.                     maxRows: 15,
  9.                     q: request.term,
  10.                     username: $location.data('username'),
  11.                     lang: $location.data('locale')
  12.                 },
  13.                 success: function(data) {
  14.                     var responseData = [];
  15.                     $.each(data.geonames, function(index, item) {
  16.                         var geoname = {
  17.                             name: "",
  18.                             adminCode1: "",
  19.                             countryCode: ""
  20.                         };
  21.  
  22.                         if (item.fclass === "P") {
  23.                             geoname.label = item.countryName + " - " + item.toponymName + " - " + item.name;
  24.                             geoname.value = item.name;
  25.                             geoname.name = item.name;
  26.                             geoname.adminCode1 = item.adminCode1;
  27.                             geoname.countryCode = item.countryCode;
  28.                         } else if (item.fcode === "ADM1") {
  29.                             geoname.label = item.countryName + " - " + item.toponymName;
  30.                             geoname.value = item.adminCode1;
  31.                             geoname.adminCode1 = item.adminCode1;
  32.                             geoname.countryCode = item.countryCode;
  33.                         } else if (item.fcode === "PCLI") {
  34.                             geoname.label = item.countryName;
  35.                             geoname.value = item.countryCode;
  36.                             geoname.countryCode = item.countryCode;
  37.                         }
  38.  
  39.                         if (item.fclass === "P" || item.fcode === "ADM1" || item.fcode === "PCLI") {
  40.                             responseData.push(geoname);
  41.                         }
  42.                     });
  43.  
  44.                     response(responseData);
  45.                 }
  46.             });
  47.         },
  48.         minLength: 3,
  49.         open: function() {
  50.             $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
  51.         },
  52.         focus: function(event, ui) {
  53.             $location.val(ui.item.label);
  54.             return false;
  55.         },
  56.         select: function(event, ui) {
  57.             $location.val(ui.item.label);
  58.             $location.siblings("[name='group[city]']").val(ui.item.name);
  59.             $location.siblings("[name='group[state]']").val(ui.item.adminCode1);
  60.             $location.siblings("[name='group[country]']").val(ui.item.countryCode);
  61.             tableFilters.refresh();
  62.             return false;
  63.         },
  64.         change: function() {
  65.             if ($location.val() === "") {
  66.                 $location.siblings("[name='group[city]']").val('');
  67.                 $location.siblings("[name='group[state]']").val('');
  68.                 $location.siblings("[name='group[country]']").val('');
  69.             }
  70.             return false;
  71.         },
  72.         close: function() {
  73.             $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
  74.         }
  75.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement