未检查Jqgrid中的复选框

时间:2016-08-05 10:23:18

标签: jquery checkbox asp.net-mvc-5 jqgrid

我正在使用 - 许可证jqGrid 4.4.4 - 带有MVC 5的jQuery Grid(版本4.4.4)。

我从我的控制器创建一个复选框列,我尝试将其设置为已选中。到目前为止,我已经尝试过:

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" });

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" });

我曾试图使用"是:否"和"真:假"同样但没有运气。我也试图通过从jqgrid调用的动作动态设置值。对于所有其他列,我收到一个值,但不是复选框。

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);
 });

我做错了什么?

更新

从服务器推迟

{"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}]}

当我在控制器中使用以下代码时

        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);

Jqgrid使用的Action

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);

和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);

它在我的屏幕上看起来像这样。复选框也已停用,但如果我使用formatoptions启用它们没有区别:{disabled:false} enter image description here

1 个答案:

答案 0 :(得分:1)

在我看来,问题的原因是代码中输入错误:您在JSON数据中使用Group1属性,在Grupp1中使用colModel。如果不是这样,那么您应该使用您使用的确切colModel附加问题的文本。