Nida Amin
Nida Amin

Reputation: 755

Response not working inside ajax autocomplete

I am using autocomplete plugin http://jqueryui.com/autocomplete/#remote-jsonp.

  $("#city" ).autocomplete({
     source: function( request, response ) {
      $.ajax({
        url: 'index.php?secController=studentProfile&action=employeeSearch',
        dataType: "JSON",
        data: {
        searchCriteria: request.term
        },
        success: function( data ) {
        console.log(data);
        response(data, function (item) {
        return {
            label: item.FulltName,
            value: item.id
        };
       });
      }
    });
  },
  minLength: 1,
  select: function( event, ui ) {
    log( ui.item ?
      "Selected: " + ui.item.label :
      "Nothing selected, input was " + this.value);
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});

The data in console are fetched with multiple json data like

     0: {_id: {…}, FullName: "Aasiya Rashid Khan", FirstMiddle: "Aasiya Rashid", FirstLast: "Aasiya Khan", Employee: {…}}
     1:{_id: {…}, FullName: "Sana Jeelani Khan", FirstMiddle: "Sana Jeelani", FirstLast: "Sana Khan", Employee: {…}}
     2:{_id: {…}, FullName: "Asad Hussain Khan", FirstMiddle: "Asad Hussain", FirstLast: "Asad Khan", Employee: {…}}

And the id of one the employee is like "_id:{$oid: "5aa75d8fd2ccda0fa0006187"}"

In the above code I am trying to return item.FullName as label of autocomplete and _id as value. They are not working. Please help!!!

Upvotes: 0

Views: 580

Answers (1)

gillyhl
gillyhl

Reputation: 475

The response callback expects one argument, here you have provided two. I think you are trying to map the returned JSON to the desired output, try:

response(
    data.map(item => ({
        label: item.FullName,
        value: item._id
    }))
)

Upvotes: 1

Related Questions