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

时间:2015-01-13 08:02:34

标签: jquery jqgrid

在我的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');
        }
    });

3 个答案:

答案 0 :(得分:3)

colNamescolModel的值应为数组相同大小的项目。您当前的代码似乎尝试使用"['ID', 'Account', 'Lob', ...]""[{ name: 'ID', key: true, hidden: true }, ...]"这样的文本分配字符串。尝试比较数组 colNamescolModel中项目数量的jqGrid代码(请参阅here)比较字符串的长度< / strong>,您将其用作colNamescolModel的值。因此显示的消息是正确的,但您的输入数据仍然是错误的。

因此,要解决此问题,您必须修复data.ColumnNamedata.ColumnModel的格式(更改类型)。

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您可以省略使用colNames数组。最好将colModel与标签集

一起使用

代码

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

相当于

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

亲切的问候