jxycms
9/13/2018 - 11:24 PM

Ajax autocomplete

$.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);
        }
    });
};