使用动态 colNames 和 colModel 时 colnames <> colmodel 的长度

Length of colnames <> colmodel when using dynamic colNames & colModel

在我的 mvc jqgrid 代码中,colnames 和 colmodel 返回相同长度的 col。但是当 运行 应用程序出现此错误时。有什么帮助吗?

代码:

public WeekColumns GetWeekColumns(DateTime start, DateTime end)
    {
        WeekColumns week = new WeekColumns();

        string sWeekDate = string.Empty;

        var model = db.GetWeek(start.ToString("MM/dd/yyyy"),
            end.ToString("MM/dd/yyyy")).ToList().Select(s => s.WeekDate.ToString());

        IEnumerable<string> sModel = new List<string>();
        sModel = model;

        string sColumnNames = "['ID', 'Account', 'Lob'";

        foreach (string item in sModel)
            sColumnNames += ", 'C" + item.ToString().Replace("/", "_").Trim() + "'";

        sColumnNames += ", 'Report']";

        string sColumnModels = "[{ name: 'ID', key: true, hidden: true }, ";
        sColumnModels += "{ name: 'Account', width: 150, align: 'center' }, ";
        sColumnModels += "{ name: 'Lob', width: 150, align: 'center' }";

        foreach (string item in sModel)
            sColumnModels += ", { name: 'C" + item.ToString().Replace("/", "_").Trim() + 
                "', width: 150, editable: true}";

        sColumnModels += ", { name: 'Report', width: 150, align: 'center' }]";

        week.ColumnName = sColumnNames;
        week.ColumnModel = sColumnModels;

        return week;
    }

js :

$.ajax({
        url: "Staffing/GetWeekDate",
        type: 'POST',
        dataType: 'json',
        contentType: "application/json",
        cache: false,
        success: function (data) {
            $("#jqTable").jqGrid({
                // Ajax related configurations
                url: "Staffing/LOBStaffing",
                datatype: "json",
                mtype: "POST",
                //ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
                jsonReader: {
                    root: 'rows',
                    page: 'page',
                    total: 'total',
                    records: 'records',
                    repeatitems: true,
                    data: 'data'
                },

                // Specify the column names
                colNames: data.ColumnName,

                // Configure the columns
                colModel: data.ColumnModel,

                // Grid total width and height  
                width: 900,
                height: 350,

                sortname: 'Lob',
                // Paging
                rowList: [],        // disable page size dropdown
                pager: $("#jqTablePager"),
                pgbuttons: false,     // disable page control like next, back button
                pgtext: null,         // disable pager text like 'Page 0 of 10'
                viewrecords: true,
                rowNum: 2000,

                grouping: true,
                groupingView: {
                    groupField: ['Account', 'Lob'],
                    groupColumnShow: [true, true],
                    groupText: ['<b>{0}</b>'],
                    groupCollapse: false,
                    groupOrder: ['asc', 'asc'],
                    groupSummary: [true, true]
                },

                // Grid caption
                caption: "List of Forms"
            }).navGrid("#jqTablePager",
                {
                    refresh: true,
                    add: false,
                    edit: false,
                    del: false,
                    search: false,
                    refreshtext: "Refresh",
                    searchtext: "Search"
                },
                {}, // settings for edit
                {}, // settings for add
                {}, // settings for delete
                {multipleGroup: true }, // settings for search,
                {
                multipleGroup: true,
                sopt: ["cn", "eq"],
                caption: "Search",
                Find: "Search"
            }); // Search options. Some options can be set on column level

        },
        error: function () {
            alert('error');
        }
    });

尝试将 colModel: data.ColumnModel 更改为 colModel: data.ColumnModel.items 来自@Oleg answer

有时我认为@Oleg 已经回答了所有 jqgrid 相关问题...

您可以省略使用 colNames 数组。最好将 colModel 与 set of label

一起使用

代码

colNames : ['My Name',....], colModel : [ {name:'myname',...}, ...],

等同于

colModel : [ {name:'myname', label: 'My Name', ...}, ...],

亲切的问候

colNamescolModel 的值应该是 数组 个相同大小的项目。似乎您当前的代码尝试使用 "['ID', 'Account', 'Lob', ...]""[{ name: 'ID', key: true, hidden: true }, ...]" 等文本分配 strings。 jqGrid 的代码试图比较 arrays colNamescolModel 中的项目数(参见 here)比较 strings 用作 colNamescolModel 的值。所以显示的信息不正确,但你输入的数据仍然是错误的。

所以要解决这个问题你必须修正 data.ColumnNamedata.ColumnModel.

的格式(更改类型

例如,您可以将 data.ColumnNamedata.ColumnModel 中的 ' 替换为 \"。它使字符串成为正确的 JSON 字符串,您可以使用 $.parseJSON()(我的意思是 colNames: $.parseJSON(data.ColumnName), colModel: $.parseJSON(data.ColumnModel))。