Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Html table having multiple rows with one drop down and one textbox control.
I want auto complete function for that text box.I implemented the following code for auto complete but it is firing for only first row.The rows are added Dynamically (in jquery) its not workig for those rows.

What I have tried:

Code:

         <table class="table table-bordered table-hover datatable-highlight" id="tWDE_Items">
                            <thead>
                                <tr>                                 
                                    <th style="display:none">ItemId</th>                                
                                    <th>Item Name</th>                                  
                                    <th>UOM</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var Item in Model.Data_Wde_ItemGrid)
                                {
                                    <tr class="datarow">
                                                                              
                                        <td style="display:none">@Item.Item_Id</td>
                                       
                                        <td>@Html.EditorFor(m => Item.Item_Name, new { htmlAttributes = new { @class = "form-control" } }) </td>
                                      
                                        <td>@Html.DropDownListFor(m => Item.UOM_Id, new SelectList(Item.UOMDetails, "UomId", "UomName"), htmlAttributes: new { @class = "form-control", id = "UomId" })</td> 
                                    </tr>
                                }
                            </tbody>
                        </table>


Jquery :

    <pre>  $('#Item_Item_Name').autocomplete({

        source: function (request, response) {
            debugger;
            var param = { ItemName: $('#Item_Item_Name').val() };
            $.ajax({
                url: "/WDE/GetAutoCompleteItemList",
                data: JSON.stringify(param),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            val: item.split('÷')[0],
                            label: item.split('÷')[1]
                           
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        change: function (e, i) {
            if (i.item) {

            }
            else {
                $('#Item_Item_Name').val('');
                $('#Item_Item_Id').val('');
            }
        },
        select: function (e, i) {
            debugger;
            $('#Item_Item_Name').val(i.item.label);
            $(this).closest("tr").find("td").eq(2).html(i.item.val);
           
        },
        minLength: 1
    });
Posted
Updated 27-Aug-18 8:08am

1 solution

try this:
$('table#tWDE_Items tbody tr td input[type="text"]').autocomplete({

        source: function (request, response) {
            debugger;
            var param = { ItemName: $('#Item_Item_Name').val() };
            $.ajax({
                url: "/WDE/GetAutoCompleteItemList",
                data: JSON.stringify(param),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            val: item.split('÷')[0],
                            label: item.split('÷')[1]
                           
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        change: function (e, i) {
            if (i.item) {

            }
            else {
                $('#Item_Item_Name').val('');
                $('#Item_Item_Id').val('');
            }
        },
        select: function (e, i) {
            debugger;
            $('#Item_Item_Name').val(i.item.label);
            $(this).closest("tr").find("td").eq(2).html(i.item.val);
           
        },
        minLength: 1
    });
 
Share this answer
 

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