【问题标题】:Checkbox in Jqgrid is not checkedJqgrid中的复选框未选中
【发布时间】:2016-08-05 10:23:17
【问题描述】:

我正在使用 - 许可 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 }

【问题讨论】:

  • 您只发布了 C# 代码片段。乍一看,它看起来是正确的。要找出问题的原因,您应该发布 JavaScript 代码并测试从服务器返回的 JSON 数据。此外,我建议您停止使用已经死了很长时间的复古版本 4.4.4。我建议您改用免费的 jqGrid 4.13.4。如果你是从 NuGet 下载的,那么你应该卸载 4.4.4 并安装 free-jqGrid 包。从CDN 加载会更好
  • 我已经做了一些更新,希望您现在能更清楚地了解情况。目前我必须处理这个旧版本的 jqGrid。

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


【解决方案1】:

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

【讨论】:

  • 是的,我的代码中有错字,但仔细查看我的 colModel,我意识到出了什么问题。在 colModel 中,我将 Name 设置为 Id,例如来自数据库的 17109,并将索引设置为例如 Grupp1。因此,当我更新将 1 或 0 放入 item.Add("17109", 1); 的代码时复选框被选中:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 2011-12-31
  • 1970-01-01
  • 2017-02-04
  • 2012-01-25
相关资源
最近更新 更多