Search code examples
javascriptjqueryjqgridasp.net-mvc-5mvcjqgrid

Dynamic columns and using dropdown list in jqGrid


I have a question regarding using a dropdown list in jqGrid. In my environment, I am using license jqGrid 4.4.4 - jQuery Grid (version 4.4.4) with MVC 5.

In my code below, I dynamically create ColNames and ColModel from my controller. Everything is working, except for the column I would like to display the values for a dropdown. Right now, the dropdown list is empty. Why is it empty?

Also, how should the values, e.g. {value:{'FE':'FedEx';'IN':'InTime';'TN':'TNT';'AR':'ARAMEX'}}, be loaded dynamically from a method in the controller?

model.CreateMeeting = false;
SecondOpinionFacade = new SecondOpinionFacade();
var meeting = SecondOpinionFacade.GetMeeting(id);
model.MeetingDay = meeting.StartDate.Date;
model.MeetingdID = meeting.SecondOpinionMeetingId;
model.FromTime = meeting.StartDate.ToString("HH:mm");
model.ToTime = meeting.EndDate.ToString("HH:mm");
model.NrofGroups = meeting.SecondOpinionGroup.Count;

var list = new[] { new { name = "SecondOpionUserId", index = "SecondOpionUserId", sorttype = "string", sortable=false, hidden = true, editable = true, edittype = "", editoptions= "", formatter = ""} }.ToList();

Here I am trying to put data into the dropdown list, but it doesn't seem to work, as the dropdown is displayed empty.

list.Add(new { name = "Deltagare", index = "Deltagare", sorttype = "string", sortable = true, hidden = false, editable = true, edittype = "select", editoptions = "{value:{'FE':'FedEx';'IN':'InTime';'TN':'TNT';'AR':'ARAMEX'}}", formatter = "select" });
int counter = 1;
meeting.SecondOpinionGroup.OrderBy(x=> x.SecondOpinionGroupId).ForEach(x =>
{
    list.Add(new {name = x.SecondOpinionGroupId.ToString(), index = "Grupp" + counter, sorttype = "string", sortable = false, hidden = false, editable = true, edittype = "checkbox", editoptions = "{ value: 'True:False' }", formatter = "checkbox" });
    counter++;
});

var list2 = new[] { "SecondOpionUserId" }.ToList();
list2.Add("Deltagare");
for (int i = 1; i <=model.NrofGroups; i++)
{
    list2.Add("Grupp"+i);
}

model.ColNames = JsonConvert.SerializeObject(list2);
model.ColModel = JsonConvert.SerializeObject(list);
return View(model);

Solution

  • You use string as the value of editoption. It's wrong. The value of editoptions should be object. Try to replace

    editoptions = "{ value: 'True:False' }"
    

    and

    editoptions = "{value:{'FE':'FedEx';'IN':'InTime';'TN':'TNT';'AR':'ARAMEX'}}"
    

    to

    editoptions = new { value = "True:False" }
    

    and

    editoptions = new { value = "FE:FedEx;IN:InTime;TN:TNT;AR:ARAMEX" }