Skip to content

Instantly share code, notes, and snippets.

@ZhenDeng
Last active May 6, 2021 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhenDeng/1b4293a62d6f166d7defd831b2aae0a7 to your computer and use it in GitHub Desktop.
Save ZhenDeng/1b4293a62d6f166d7defd831b2aae0a7 to your computer and use it in GitHub Desktop.
$.fn.showPostcodeAutoComplete = function (pcode) {
console.log(pcode);
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: 'api/checkout/GetPostCodeList',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(pcode),
dataType: 'json',
asyn: false,
success: function (output) {
console.log(output);
response($.map(output, function (item) {
var displayLabel;
if (item === "No match found")
displayLabel = "No match found";
else {
if (item.companyCode === '1') {
displayLabel = item.suburb + ", " + item.state + ", " + item.postcode;
}
else {
displayLabel = item.suburb + ", " + item.postcode;
}
}
return {
suburb: item.suburb,
state: item.state,
postcode: item.postcode,
label: displayLabel
};
})
);
} // end success
}); // end ajax
},
minLength: 2,
delay: 0,
autoFocus: true,
select: function (event, ui) {
ui.item.value = ui.item.postcode;
$('#suburbField').val(ui.item.suburb);
$('#stateField_Input').val(ui.item.state);
$('#stateField').val(ui.item.state);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment