0

I am generating dynamic textboxes for autocomplete depending on user input.

var projects = [
        {
            label: "Test12",
            desc: "responsive web application kit"
        },
        {
            label: "Londinium",
            desc: "responsive bootstrap 3 admin template"
        },
        {
            label: "It's Brain",
            desc: "Bootstrap based "
        }
    ];

    // Initialize autocomplete
    $(document).on('click', '.ac-custom', function () {
        $(this).autocomplete({
            minLength: 0,
            source: function (request, response)
            {
                $.ajax({
                    url: "/Home/GetInfo",
                    type: "POST",
                    dataType: "json",
                    data: { Name: $(this).val() },
                    success: function (data) {


                    }
                });
            },
            focus: function (event, ui) {
                $(this).val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $(this).val(ui.item.label);
                return false;
            }
        })
        .autocomplete("instance")._renderItem = function (ul, item) {
            return $("<li>").append("<span class='text-semibold'>" + item.label + '</span>' + "<br>" + '<span class="text-muted text-size-small">' + item.desc + '</span>').appendTo(ul);
        }
    });

If I give the source as projects this works but I need to fetch from database so I am calling an action method but somehow this is not working. Is it because I am binding ajax call to controls created at runtime. Your help is appreciated. Thanks

2 Answers 2

1

You need pass data in response from success callback

// Initialize autocomplete
    $(document).on('click', '.ac-custom', function () {
        $(this).autocomplete({
            minLength: 0,
            source: function (request, response)
            {
                $.ajax({
                    url: "/Home/GetInfo",
                    type: "POST",
                    dataType: "json",
                    data: { Name: $(this).val() },
                    success: function (data) {
                        response( data );
                    },
                    error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                          msg = 'Not connect. Verify Network.';
                        } else if (jqXHR.status == 404) {
                          msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                          msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                          msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                          msg = 'Time out error.';
                        } else if (exception === 'abort') {
                          msg = 'Ajax request aborted.';
                        } else {
                         msg = 'Uncaught Error.';
                        }

                       alert(msg + "<br/>responseText: " + jqXHR.responseText);
                     }
                });
            },
            focus: function (event, ui) {
                $(this).val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $(this).val(ui.item.label);
                return false;
            }
        })
        .autocomplete("instance")._renderItem = function (ul, item) {
            return $("<li>").append("<span class='text-semibold'>" + item.label + '</span>' + "<br>" + '<span class="text-muted text-size-small">' + item.desc + '</span>').appendTo(ul);
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that's fine but call is not made to "/Home/GetInfo" so there is nothing in response
Add error callback and let us know what is the error? I have updated code and added error callback
1

Update you source function like this

source: function (request, response) {
                $.ajax({
                    url: '/Home/GetInfo',
                    data: { 'text': $.trim($('#yourtextboxid').val()) },
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.name,
                                id: item.id
                            }
                        }));
                    }
                })
            }

This will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.