Last few days i was trying to get jqgrid with autocompletion fields to work, now i can get it to work with local data, but as soon as i trying to get data from my controller data didnt get parsed.
View code:
{ name: 'EanNummer', index: 'EanNummer', width: 65, sortable: true, editable: true, edittype: 'text', editoptions: {
dataInit:
function (elem) {
$(elem).autocomplete({ minLength: 0, source: '@Url.Action("GetBrands")' })
.data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.Id + ", " + item.Name + "</a>")
.appendTo(ul);
};
}
}
},
if instead of source: url i use source: [“c++”, “java”, “php”, “coldfusion”, “javascript”, “asp”, “ruby”] for example code works fine and shows up, so something must be wrong with my controller side code
Controller Code:
public JsonResult GetBrands()
{
string vendorId = "";
var username = "";
var name = System.Web.HttpContext.Current.User.Identity.Name;
var charArray = name.Split("\\".ToCharArray());
username = charArray.Last();
vendorId = service.GetVendorIdByUsername(username);
List<String> list = new List<String>();
var brands = service.getBrandsByVendor(vendorId);
var s= (from brand in brands
select new
{
Id = brand.BrandId,
Name = brand.BrandName
}).ToList();
return Json(s);
}
If you use
item.Idanditem.Nameon the client side you should return not theList<String>. Instead of that you should returns the list ofnew {Id=brand.BrandId, Name=brand.BrandName}. You should just use LINQ instead offoreach:UPDATED: I modified for you the demo from the answer and included jQuery UI Autocomplete support in two forms. The standard rendering:
and the custom rendering:
The Autocomplete functionality works in Advanced Searching dialog in the same way like in the Searching Toolbar:
You can download the demo from here.
The server code of the standard autocomplete is
It returns array of strings in JSON format. The list of Titles are filtered by
termargument which will be initialized to the string typed in the input field.The server code of the custom autocomplete is
It uses SqlFunctions.StringConvert to be able to use
LIKEin comparing of the integers. Moreover it returns the list of objects havingvaluethetitleproperty. If you would return objects havingvaluethelableproperties the values from thelableproperties will be displayed in the Autocomplete context menu and the correspondingvalueproperty will be inserted in the input field. We use customtitleproperty instead.The code of the client side is
for the standard rendering and
for the custom rendering.
Additionally I use some CSS settings:
You can uncomment
.ui-autocomplete.ui-menu { opacity: 0.9; }setting if you want to have some smallopacity effect in the autocomplete context menu.