在jqgrid中设置C#的下拉选项

时间:2012-11-08 07:14:26

标签: c# jquery jqgrid jqgrid-asp.net

我使用C#在服务器端生成列模型并将jSON传递给网格。现在我想要一个列来提供下拉功能。但它没有成功,这是我到目前为止所做的:

StringBuilder deptDetails = new StringBuilder();
foreach (Department dept in departmentList)
{
    deptDetails.Append(dept.DepartmentID);
    deptDetails.Append(":'");
    deptDetails.Append(":'" + dept.DepartmentName + "',");
    deptDetails.Append(',');
}
deptDetails.Length -= 1;

colModel[i] = new ColumnMod
{
    name = selectedItems[i].ToString(),
    index = selectedItems[i].ToString(),
    width = 100,
    editable = true,
    edittype = "select",
    align = "left",
    editoptions="{value:{" + deptDetails + "}}"
 };

这是我的columnmodel类:

public class ColumnMod
{
    public string name { get; set; }
    public string index { get; set; }
    //public Boolean key { get; set; }
    public Int32 width { get; set; }
    public Boolean editable { get; set; }
    public string edittype { get; set; }
    //public Boolean sortable { get; set; }
    public string align { get; set; }
    //public Boolean hidden { get; set; }
    public string editoptions { get; set; }
}

编辑:

这是我的JS代码:

$grid.jqGrid({
    colNames: selectedItems,
    colModel: columnSelected,
    sortname: selectedValues[0],
    sortorder: "asc",
    editurl: 'url',
    cellsubmit: 'clientArray',
    cellEdit: true,
    rowNum: 5000,
    rownumbers: true,
    rownumWidth: 30,
    autowidth: true,
    shrinkToFit: true,
    gridview: true,
    pager: '#gridPager',
    viewrecords: true,
    recordtext: "Total Rows: {2}",
    jsonReader: {
        root: "rows",
        page: "page",
        total: "totalpages",
        records: "totalrecords",
        cell: "cell",
        id: selectedValues[0], //index of the column with the PK in it 
        userdata: "userdata",
        repeatitems: true
    },
    prmNames: {
        rows: "numRows",
        page: "page",
        sort: "sortField",
        order: "sortOrder"
    },
    datatype: function (postdata) {
        if (lastSortField != postdata.sortField || lastSortOrder != postdata.sortOrder)
         {   if (!$('#btnValidate').is(':disabled')) {
                lastSortField = postdata.sortField;
                lastSortOrder = postdata.sortOrder;
            }
        }
    }

这就是我在columnModel中获取值的方法:

function getSelectedColumnsJSON(selectedValues) {
try {
    $.ajax({
        type: "POST",
        url: window.location.protocol + '//' + window.location.host + window.location.pathname + "/getSelectedColumnsJSON",
        data: $.toJSON({
            selectedItems: selectedValues
        }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (data, status) {
            if (data.d)
                columnSelected = data.d;
        },
        error: function (error) {
            ShowMessage(error.responseText);
        }
    });
}
catch (ex) {
    ShowMessage(ex.message);
}

}

我在列中收到了下拉列表选项,但它是空的,我尝试按照 here 解释的格式提供帮助吗?

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解您的问题,因为您没有发布用于填充网格的 JSON数据。您是否在输入数据中发布了DepartmentIDDepartmentName个值?可能你需要另外使用formatter:'select'它会解决你现有的问题吗?

我不了解您需要实现的完整方案,但通常最好使用dataUrl而不是使用editoptionsvalue属性。可能是你必须使用value属性的唯一情况是使用formatter:'select'。仅当从服务器返回的数据包含id而不是字符串数据时,才能使用选项dataUrl