Domanda

I am passing complex JSON data to jQuery autocomplete plugin. And it is working fine so it shows the list of Products.

enter image description here

Now I want to get somehow Price that is already included into JSON data and when I select product from autocomlete list I would like to populate input tag with Price.

I really cannot get if it is possible to do. What I know that data is already in JSON but how to get it?

Any clue?

Here is JS for jQuery autocomplete plugin

 function CreateAutocomplete() {

        var inputsToProcess = $('[data-autocomplete]').each(function (index, element) {
            var requestUrl = $(element).attr('data-action');

            $(element).autocomplete({
                minLength: 1,
                source: function (request, response) {
                    $.ajax({
                        url: requestUrl,
                        dataType: "json",
                        data: { query: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.Name,
                                    value: item.Name,
                                    realValue: item.UID                                   
                                };
                            }));
                        },
                    });
                },
                select: function (event, ui) {

                    var hiddenFieldName = $(this).attr('data-value-name');
                    $('#' + hiddenFieldName).val(ui.item.UID);
                }
            });
        });
    }

To make clear item.LastPrice has Price data.

And HTML

   @Html.AutocompleteFor(x => x.ProductUID, Url.Action("AutocompleteProducts", "Requisition"), true, "Start typing Product name...", Model.Product.Name)
È stato utile?

Soluzione

In your ui.item object you should be able to find the the Price property in there and then set the value in the select function.

success: function (data) {
    response($.map(data, function (item) {
        return {
            label: item.Name,
            value: item.Name,
            realValue: item.UID,
            price: item.LastPrice // you might need to return the LastPrice here if it's not available in the ui object in the select function
        };
    }));
},
..

select: function (event, ui) {
   var hiddenFieldName = $(this).attr('data-value-name'),
       unitPriceEl = $('#price');
   $('#' + hiddenFieldName).val(ui.item.UID);
   unitPriceEl.val(ui.item.LastPrice); //set the price here
}

Altri suggerimenti

Thanks to dcodesmith!!! I am gonna mark his solution like an answer but just in case I will share my final code that is working fine now.

function CreateAutocomplete() {

        var inputsToProcess = $('[data-autocomplete]').each(function (index, element) {
            var requestUrl = $(element).attr('data-action');

            $(element).autocomplete({
                minLength: 1,
                source: function (request, response) {
                    $.ajax({
                        url: requestUrl,
                        dataType: "json",
                        data: { query: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.Name,
                                    value: item.Name,
                                    realValue: item.UID,
                                    lastPrice: item.LastPrice
                                };
                            }));
                        },
                    });
                },
                select: function (event, ui) {

                    var hiddenFieldName = $(this).attr('data-value-name');
                    $('#' + hiddenFieldName).val(ui.item.UID);

                    var unitPriceEl = $('#UnitPrice');
                    unitPriceEl.val(ui.item.lastPrice);
                    console.log(ui.item.lastPrice);
                }
            });
        });
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top