Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var $location = $("#location");
- $location.autocomplete({
- source: function(request, response) {
- $.ajax({
- url: "//ws.geonames.org/searchJSON",
- dataType: "jsonp",
- data: {
- maxRows: 15,
- q: request.term,
- username: $location.data('username'),
- lang: $location.data('locale')
- },
- success: function(data) {
- var responseData = [];
- $.each(data.geonames, function(index, item) {
- var geoname = {
- name: "",
- adminCode1: "",
- countryCode: ""
- };
- if (item.fclass === "P") {
- geoname.label = item.countryName + " - " + item.toponymName + " - " + item.name;
- geoname.value = item.name;
- geoname.name = item.name;
- geoname.adminCode1 = item.adminCode1;
- geoname.countryCode = item.countryCode;
- } else if (item.fcode === "ADM1") {
- geoname.label = item.countryName + " - " + item.toponymName;
- geoname.value = item.adminCode1;
- geoname.adminCode1 = item.adminCode1;
- geoname.countryCode = item.countryCode;
- } else if (item.fcode === "PCLI") {
- geoname.label = item.countryName;
- geoname.value = item.countryCode;
- geoname.countryCode = item.countryCode;
- }
- if (item.fclass === "P" || item.fcode === "ADM1" || item.fcode === "PCLI") {
- responseData.push(geoname);
- }
- });
- response(responseData);
- }
- });
- },
- minLength: 3,
- open: function() {
- $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
- },
- focus: function(event, ui) {
- $location.val(ui.item.label);
- return false;
- },
- select: function(event, ui) {
- $location.val(ui.item.label);
- $location.siblings("[name='group[city]']").val(ui.item.name);
- $location.siblings("[name='group[state]']").val(ui.item.adminCode1);
- $location.siblings("[name='group[country]']").val(ui.item.countryCode);
- tableFilters.refresh();
- return false;
- },
- change: function() {
- if ($location.val() === "") {
- $location.siblings("[name='group[city]']").val('');
- $location.siblings("[name='group[state]']").val('');
- $location.siblings("[name='group[country]']").val('');
- }
- return false;
- },
- close: function() {
- $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement