How to execute on change before autocomplete where file number is match

Ahmed Abd El Aziz 315 Reputation points
2023-08-30T23:03:56.5033333+00:00

I working on asp.net mvc . i face issue autcomplete execute before onchange on all cases 

I don't need that .

What I need is when employee id matched on database meaning exist only one on database 

then onchange working before autocomplete 

if more than one case what entered then display autocomplete 

as sample

if i have on employee id on database

1234590

1234591

1234592

so when write on input text box 1234 so will display 

1234590

1234591

1234592

and when write 1234590 

so no need to display autocomplete only display name based on fire on change because it have only one value

this mean display on change will fire on this case before autocomplete 

$("#txtLineManagerId").autocomplete({
            source: function (request, response) {
                var searchText = $("#txtLineManagerId").val();
                console.log("search text" + searchText)
                $.ajax({
                    url: '@Url.Action("GetAllEmployeeBasedSearchText", "Resignation")',
                    data: { searchText: searchText },
                    method: "GET",
                    dataType: "json",
                    success: function (data) {

                        response($.map(data, function (item) {
                            console.log("data is" + item.EmployeeID);

                            return { label: item.EmployeeID, value: item.EmployeeID, employeeName: item.EmployeeName };

                        }))

                    }
                });
            },
            position: { my: "right top", at: "right bottom" },
            appendTo: '#searchContainer',
            select: function (event, ui) {
                // Update LineManagerName input with the selected Employee Name
                $("#LineManagerName").val(ui.item.employeeName);
                $("#selectedEmployeeName").val(ui.item.employeeName);
            },
            minLength: 4,

        });

                     $("#txtLineManagerId").change(function () {
            var employeeid = $("#txtLineManagerId").val();
            console.log("search text" + employeeid)
            $.ajax({
                url: '@Url.Action("GetEmployeeName", "Resignation")',
                data: { employeeid: employeeid },
                method: "GET",
                dataType: "json",
                success: function (data) {
                    $("#LineManagerName").val(data);
                    console.log("data is " + data);
                  

                }
            });

      });
 

so How to do that please 

my scripts details

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,492 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,426 Reputation points Microsoft Vendor
    2023-08-31T07:24:38.3633333+00:00

    Hi @Ahmed Abd El Aziz,

    I don't quite understand why you want to do this because when you enter the full employee ID(1234590 ), the autocomplete feature only shows one.

    User's image

    If you insist on using it, you can judge it in the change event.

     success: function (data) {
         console.log("search text" + data)
         if (data != "") {
             $("#LineManagerName").val(data);
    
         }
         else {
             Test();
         }
     }
    

    Then put $("#txtLineManagerId").autocomplete into the test() method,and clear the previous LineManagerName content,

     function Test() {
        
         $("#LineManagerName").val("");
         $("#txtLineManagerId").autocomplete({
             //......
            
         });
     }
    

    5

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.