user3197410
user3197410

Reputation: 133

Checkbox in Jqgrid is not checked

I am using - license jqGrid 4.4.4 - jQuery Grid (version 4.4.4) with MVC 5.

I create a checkbox column from my controller which I try to set as checked. So far I have tried:

list.Add(new {name = x.SecondOpinionGroupId.ToString(), index = "Grupp" + counter, sorttype = "string", sortable = false, hidden = false, editable = true, edittype = "checkbox", editoptions = new { value = "1:0", defaultValue ="1" }, formatoptions = new { disabled = "false", value="0:1" }, formatter = "checkbox" });

And

list.Add(new {name = x.SecondOpinionGroupId.ToString(), index = "Grupp" + counter, sorttype = "string", sortable = false, hidden = false, editable = true, edittype = "checkbox", editoptions = new { value = "1:0", defaultValue ="1" }, formatter = "checkbox" });

I have tried to use "Yes:No" and "True:False" as well but no luck. I am also trying to set the value dynamically by the action that is called from jqgrid. For all other columns i recieve a value but not for the checkbox.

users.Item1.ForEach(x => {
  int counter = 1;

  var item = new Dictionary < string,
   object > ();
  item.Add("SecondOpionUserId", x.UserID);
  item.Add("Deltagare", x.UserID);
  meeting.SecondOpinionGroup.OrderBy(y => y.SecondOpinionGroupId).ForEach(z => {

   item.Add("Grupp" + counter, "1");
   counter++;
  });

  jsonData.Data.Add(item);
 });

What am I doing wrong?

Update

Respone from server

{"records":2,"total":1,"Page":1,"PageSize":10,"SortColumn":"FullName","Data":[{"SecondOpionUserId":2113,"GroupId":17109,"Deltagare":2113,"Group1":1},{"SecondOpionUserId":1005,"GroupId":17110,"Deltagare":1005,"Group1":1}]}

When I am using the following code in controller

        model.CreateMeeting = false;
            SecondOpinionFacade = new SecondOpinionFacade();
            //var meeting = SecondOpinionFacade.GetMeeting(id);
            var meeting = SecondOpinionFacade.GetMeeting(17061);
            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= new { value = ""}, formatter = ""} }.ToList();
            list.Add(new { name = "GroupId", index = "GroupId", sorttype = "string", sortable = true, hidden = false, editable = true, edittype = "", editoptions = new { value = "" }, formatter = "" });
            list.Add(new { name = "Deltagare", index = "Deltagare", sorttype = "string", sortable = true, hidden = false, editable = true, edittype = "select", editoptions = new { value = GetAllUsers() }, 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 = new { value = "1:0" }, formatter = "checkbox" });
                //list.Add(new { name = x.SecondOpinionGroupId.ToString(), index = "Grupp" + counter, sorttype = "string", sortable = false, hidden = false, editable = true, edittype = "", editoptions = new { value = "True:False" }, formatter = "myUrlFormatter" });
                counter++;

            });

            var list2 = new[] { "SecondOpionUserId" }.ToList();
            list2.Add("GroupId");
            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);

And Action that is used by Jqgrid

var jsonData = new
            {
                records = meetingUsers.Item2,
                total = (meetingUsers.Item2 + rows - 1) / rows,
                Page = page,
                PageSize = rows,
                SortColumn = sidx,
                Data = new List<Dictionary<string, object>>()

            };

            meetingUsers.Item1.ForEach(x =>
            {               
                var item = new Dictionary<string, object>();
                item.Add("SecondOpionUserId", x.UserId);
                item.Add("GroupId", x.SecondOpinionGroupId);
                item.Add("Deltagare", x.UserId);
                item.Add("Group1", 1);
                jsonData.Data.Add(item);
            });

            return Json(jsonData, JsonRequestBehavior.AllowGet);

And jqGrid

   currentGrid.jqGrid({
            url: '@Url.Action("jqGridMeetingList", "SecondOpinion")',
            editurl: '@Url.Action("jqGridMeetingList", "SecondOpinion")',
            caption: "SecondOpionMöte",
            loadonce: false,
            datatype: "json",
            height: "100%",
            width: 1200,
            shrinkToFit: true,
            autowidth: true,
            colNames: @(Html.Raw(Model.ColNames)),
            colModel: @(Html.Raw(Model.ColModel)),
            postData: getForm() @*{meetingId : @(Model.MeetingdID)}*@,
            serializeRowData: function(postdata){
                return { rowInfo: JSON.stringify(postdata) };
            },
            jsonReader: {
                root: 'Data',
                repeatitems: false
            },
            rowNum: 10,
            rowList: [10, 20, 50],
            pager: '#pager_SecondOpinionGrid',
            sortname: 'FullName',
            toolbarfilter: true,
            viewrecords: true,
            sortorder: "desc",
            ////så att den inte laddar vid postback (init)
            //loadBeforeSend: function (xhr, settings) {
            //    this.p.loadBeforeSend = null; //remove event handler
            //    return false; // dont send load data request
            //},
            gridComplete: function() {
                //$(".printButtons").attr("disabled", true);
            },
            beforeProcessing: function(data) {
                if (data.Data === undefined) {
                    showError(data.Error);
                    return false;
                }
            },
            loadComplete: function() {
                storeSortingAndPagingToViewModel();
                resizeIframe();
                //checkboxes();
                addGroupButtons();
             },
            onSelectRow: function(rowid, e) {
                //var blnChecked = $(e.target).is(":checked");
                var trElement = $("#" + rowid, jQuery('#SecondOpinionGrid'));

                //Om man valt samma rad avmarkera den
                if (trElement.hasClass("selectedJqGridRow")) {
                    trElement.removeClass('selectedJqGridRow');
                    currentGrid.jqGrid('resetSelection');
                } else {
                    trElement.addClass('selectedJqGridRow');
                }

                //Ta bort tidigare markering
                if (selectedRowId !== 0 && selectedRowId !== rowid) {
                    $("#" + selectedRowId, jQuery('#SecondOpinionGrid')).removeClass('selectedJqGridRow');
                }
                selectedRowId = rowid;
                //debugger;
                //$("table#SecondOpinionGrid input:checkbox").each(function(ch) {
                //    var $this = $(this);
                //    $this.replaceWith('<input type="radio" name ="'+ $this.closest('tr').find('td[aria-describedby="SecondOpinionGrid_SecondOpionUserId"]').attr('title')+'" data-secopgrid="'+$this.closest('td').attr('aria-describedby').split("_")[1]+'" />');

                });

            }
        });

        var inlineparams = {
            addParams: {keys: false},
            refresh: false,
            edit: true,
            edittext: 'Editera',
            add: true,
            addtext:'Lägg till',
            search: false,
            cancel: false,
            save:true,
            savetext:'Spara',
            editParams: {
                keys: false,
                url: '@Url.Action("SaveRow", "SecondOpinion")',
                oneditfunc:editFunc,
                aftersavefunc: afterSaveFunc,
                extraparam:{ page :$('#SecondOpinionGrid').getGridParam('page'),
                    sortorder: $('#SecondOpinionGrid').getGridParam('sortorder'), meetingId : '@(Model.MeetingdID)'}
            }
        };

        currentGrid.jqGrid('navGrid', '#pager_SecondOpinionGrid', { deltext:'Ta bort', edit:false, add:false, search: false, refresh: false });
        currentGrid.jqGrid('inlineNav', "#pager_SecondOpinionGrid", inlineparams);

And it looks like this on my screen. The checkboxes is disabled as well, but no difference if I enable them with formatoptions: { disabled: false } enter image description here

Upvotes: 0

Views: 1155

Answers (1)

Oleg
Oleg

Reputation: 222017

It seems to me that the reason of your problem is typing error in your code: you use Group1 property in the JSON data and Grupp1 in the colModel. If it's not so then you should append the text of your question with exact colModel which you use.

Upvotes: 1

Related Questions