views:

545

answers:

1

I'm using the autocomplete jQuery plugin (api doc) that calls a service which returns json. I have the plugin consuming the json result fine and everything works, execpt my custom formatResult function is never called. I'd like to use that function to capture the user selecting from the result list to populate various other fields on the page.

$(document).ready(function() {
    $("#vendorname").autocomplete("/Vendor/Search", {
        dataType: 'json',
        parse: function(data) {
            var rows = new Array();
            for (var i = 0; i < data.length; i++) {
                rows[i] = { data: data[i], value: data[i].Name, result: data[i].Name };
            }
            return rows;
        },
        formatItem: function(row, i, n) {
            return row.RepName + ' (' + row.VendorId + ')';
        },
        formatResult: function(row, i, n) {
            return '(formatResult) ' + row.Name;
        },
        width: 200,
        mustMatch: false,
        scroll: true,
        scrollHeight: 300
    });
});

The json string returned from the service is (two Vendor objects)

[{"VendorId":1,"Name":"abc","RepName":"rep of abc","Phone":null,"Email":null,"Notes":null,"Version":null,"Fax":null,"ProductQuotes":[],"PurchaseOrders":[]},{"VendorId":2,"Name":"def","RepName":"rep of def","Phone":null,"Email":null,"Notes":null,"Version":null,"Fax":null,"ProductQuotes":[],"PurchaseOrders":[]}]

So,

1) Why is formatResult is never hit?

2) Is there a better way to grab the object properties once a user selected an item from the results?

A: 

The format result just changes what shows up in the text box. If you want to intercept changes to the value in the input then do

 $("#vendorname").result(function(event, data, formatted){
   //do something
  });
stimms