Click here to Skip to main content
15,312,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I need to autocomplete and selected value changed in webgrid: i have done but the selected value changed is executes final row only... didn't value change at every rows.



With regards,
Vivek .R

What I have tried:

JavaScript
//Autocomplete
    var setupAutoComplete = function () {
        var r = 0;
        $(".ItemName").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/MRD/GetData?term=" + request.term,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.length > 0) {
                            response($.map(data, function (item) {
                                return { label: item.ItemWithRate, value: item.ItemWithRate
                                };
                            }))
                        }
                        else {
                            response([{ label: 'No results found.', val: -1}]);
                        }
                    }
                })
            },
            select: function (event, ui) {
                var itemname = ui.item.value;
                if (ui.item.val == -1) {
                    ic.val('');
                    rc.val('');
                    iname.val('');
                    return false;
                }
                else {
                    $.getJSON('@Url.Action("GetDetail")', { itemname: ui.item.value }, function (data) {
                        if (data) {
                         
                           
             var itcode =  $('.ItemCode').closest("tr").find('.ItemCode').last();
             var ircode = $('.IRCode').closest("tr").find('.IRCode').last();
             var unitprice = $('.UnitPrice').closest("tr").find('.UnitPrice').last();

             ircode.val(data.rcode);
             itcode.val(data.icode);
             unitprice.val(data.unitprice);
                        }
                    });
                }
            },
            //  change: txtOneChanged,    
            messages: {
                noResults: "", results: ""
            }
        });
    }
Posted
Updated 23-Nov-16 23:34pm
v2

Quote:
i have done but the selected value changed is executes final row only
What else should the following code do?
var itcode =  $('.ItemCode').closest("tr").find('.ItemCode').last();
var ircode = $('.IRCode').closest("tr").find('.IRCode').last();
var unitprice = $('.UnitPrice').closest("tr").find('.UnitPrice').last();
 
ircode.val(data.rcode);
itcode.val(data.icode);
unitprice.val(data.unitprice);

You are telling JavaScript to use the last element in the collection, remove that and use something else, try to use an index.

Tip: Start by the top level e.g. <table> and find the row (<tr>) you want to target, you can do easily by passing the index or calculating that.
   
Comments
Vivek.anand34 23-Nov-16 6:47am
   
how to find the row....
Afzaal Ahmad Zeeshan 23-Nov-16 6:51am
   
You can get the index of the element in focus, that way get the row. Or just use $(this) to target current element.
Vivek.anand34 23-Nov-16 7:25am
   
please can u send that code...
Vivek.anand34 24-Nov-16 0:23am
   
Hi.. anyone help this concept.still didn't solve this..
AS u are binding autocomplete with class.
// run when eventlistener is triggered
$(".ItemName").on( "autocompletechange", function(event,ui) {
// post value to console for validation
console.log($(this).val());
});
you can get the selected value.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900