I have some data which I am formatting like this:
// ... imagine populating a SqlDataReader with some results ...
var results = new StringBuilder();
while (reader.Read())
{
results.AppendFormat("{0}, {1}\n", reader["name"], reader["emailAddress"]);
}
return results.ToString();
My controller action is pretty simple:
public ActionResult Find(string q)
{
var users = Customer.Search(q);
return Content(users);
}
And my javascript in the view looks like this:
$(document).ready(function() {
$("input#user").autocomplete('<%= Url.Action("Find", "Customer") %>', {
minChars: 2,
width: 500,
matchContains: true,
autoFill: false,
formatItem: function(row, i, max) {
return i + "/" + max + ": (" + row[0] + ") " + row[1];
},
formatMatch: function(row, i, max) {
return row[0];
},
formatResult: function(row) {
return row[1];
}
});
});
Question A
I am using the Autocomplete from here. At this point I am having an issue where I cannot get the two fields to read as separate values. For example, if a rows name field is "John" and its email field "[email protected]" I would expect those to show up in row[0] and row[1] respectively. However, they currently I get "John, [email protected]" in row[0] and row[1] is undefined.
What do I need to change (either in the javascript or the method where I'm building the string) to get row[0] and row[1] to show the proper data?
Question B
I would prefer to have the data in the rows named. By this I mean:
formatItem: function(row, i, max) {
return i + "/" + max + ": (" + row.name + ") " + row.email;
I struggled for a while to format my data so this would happen but I was never successful. How would I format my data so that the AutoComplete would understand this?