0

Yeah, there are many questions in the forum but somehow none works for me. I am not doing very special.

My Autocomplete is populating the results but the results are not getting filtered

My code is :

$("#empName").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: "api/clientEmp/of/" + sessionClientId + "/notingroup/" + groupId,
            type: "GET",
            dataType: "json",
            data: {
                term: $("#empName").val()
            },
            success: function(data) {
                response($.map(data, function(item) {
                    return {
                        label: item.EmpName,
                        value: item.EmpId
                    };
                }));
            }
        });
    },
    minLength: 0,
    focus: function(event, ui) {
        $("#empName").val(ui.item.label);
    }
});    

And the json output is something like this

[
    {
        "EmpId": 26,
        "EmpNum": "Rel-72015-31",
        "EmpName": "R.durai"
    },
    {
        "EmpId": 21,
        "EmpNum": "REL-42015-22",
        "EmpName": "Zishan"
    },
    {
        "EmpId": 56,
        "EmpNum": "Rel-22015-19",
        "EmpName": "Raj Singh"
    }
]

It neatly shows the options but when i type anything this all results shows up and they are not filtered

I am missing a piece of code which asks to filter the results. Can anyone please suggest.

thanks in advance.

4
  • do u see any error on console? Commented Sep 3, 2015 at 11:59
  • no, i dont see any error in the console. Commented Sep 3, 2015 at 12:01
  • Do you actually filter anything on the server? Commented Sep 3, 2015 at 12:08
  • No.the query on the server is from around 35 tables. and at any given point of time the resultset will have not more than 10 records. So, I have stored procedure executed on the api call get the results in json and want populate the autocomplete Commented Sep 3, 2015 at 12:29

1 Answer 1

6
$("#empName").autocomplete({
 source: function(request, response) {
    $.ajax({
        url: "api/clientEmp/of/" + sessionClientId + "/notingroup/" + groupId,
        type: "GET",
        dataType: "json",
        data: {
            term: $("#empName").val()
        },
        success: function(data) {
            var array = $.map(data, function(item) {
                return {
                    label: item.EmpName,
                    value: item.EmpId
                };
            });

             //call the filter here
                response($.ui.autocomplete.filter(array, request.term));
        }
    });
},
         minLength: 0,
      focus: function(event, ui) {
        $("#empName").val(ui.item.label);
      }
   }); 
Sign up to request clarification or add additional context in comments.

1 Comment

How/why does this work? Previously I've sent back response(arrayHere); - all the resuts were there but it didn't filter and by changing the code to your answer it works - can you please explain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.