Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery autocomplete textbox set value programmatically

I am using jquery 1.8.2 and jqueryui 1.9.0 libraries.

I am trying to add an "Edit Record" dialog to my table which contains a autocomplete textbox of jquery-ui.

I am also using the change event of autocomplete to restrict the user for selecting the items from the given list.

My problem is: when I show my edit form, I am setting the existing data to the textbox, however when user clicks on another field in edit form, the change controller popping the error of nothing changed. I have found many samples which show how to set value for combobox type autocomplete but i could not find one for textbox type. Please help me on this issue, i believe your answers will be useful for other users too.

here is my code for autocomplete textbox:

    $("#EditMaterialName").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                url: "someurl",
                dataType: "json",
                data: {
                    search_string: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.description,
                            value: item.description
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {

        },
        change: function (event, ui) {
            if (!ui.item) {
                $('<div class="error"><b> please select a listed value.</b><div>').insertAfter('#EditMatName');
                $('#EditMaterialName').val('');
            }
        },
        open: function () {
            $(this).removeClass('ui-corner-all').addClass('ui-corner-top');

        },
        close: function () {
            $(this).removeClass('ui-corner-top').addClass('ui-corner-all');

        }
    });

and here is the code for showing Edit Record dialog:

$('a.edit_link').live("click", function () {

        clearEditMaterialForm();

        //loading selected value
        var valMaterialName = $(this).closest('tr').children('td.cl_material_name').text();

        $('#EditMaterialName').val(valMaterialName);

    });

thank you very much for your answers.

like image 455
Delidervis Avatar asked Sep 07 '13 17:09

Delidervis


1 Answers

It's not beautiful, maybe it's not correct but it's working:

function setAutocompletCurrentValue(id, value) {
   $(id).val(value);
   var textToShow = $(id).find(":selected").text();
   $(id).parent().find("span").find("input").val(textToShow);
}

Hope it helps.

like image 127
GRGodoi Avatar answered Oct 17 '22 03:10

GRGodoi