jqgrid colmodel editoptions加载json结果

时间:2016-12-27 08:21:37

标签: jqgrid

我想在我的jqgrid的DropDown中加载我的服务器数据。我的代码,

更新代码:

 public ActionResult GetUnit()
    {
        List<UnitModel> getUnitValues = new List<UnitModel>();
        //ToDo db code
        Dictionary<int, string> unitValues = new Dictionary<int, string>();
        unitValues = getUnitValues.ToDictionary(x => x.UnitID, x => x.UnitDesc);
        unitValues.Add(4, "Unit2/3");
        unitValues.Add(1, "Unit1");
        unitValues.Add(2, "Unit2");
        unitValues.Add(3, "Unit3");
        return Json(unitValues, JsonRequestBehavior.AllowGet);
    }

我的jqgrid:

     colModel: [...
      {
            name: 'UnitID', index: 'UnitID', editable: true, edittype: 'select', width: "200",
            formatter: 'select', editoptions: { value: unitslist},
            editrules: { custom: true, custom_func: dupicateRecordValidation
            }
        },
...],

       beforeProcessing: function () {
                    $.ajax({
                    url: '/Home/GetUnit/',
                    dataType: 'json',
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        $.map(data, function (value, key) {
                            unitsList += '"' + value + '"' + ':' + '"' + key + '"' + ',';
                        });
                        unitsList += '}';
                        alert(unitsList);
                    }
                });
            },

但是,这不起作用。 jqgrid DropDown列加载了空单元格。我错过了什么吗?这是正确的方法吗?请建议是否有任何替代方法来加载jqgrid下拉列表与服务器数据,并选择该行的默认值。感谢。

注意:我使用的是Jquery jqgrid v4.4.4 Visual Studio

1 个答案:

答案 0 :(得分:2)

首先,了解何时应使用目前使用的formatter: 'select'非常重要。如果您想在UnitID中填充包含ID信息的网格,则需要这样做,但您需要显示与ids对应的文本。例如,从服务器获取的JSON数据可以包含属性language,内容为"de""en""fr"等等,但您需要要显示在"German"列而不是"de""English"而不是"en""French"而不是"fr"。如果你应该定义

formatter: 'select', editoptions: { value: 'de:German;en:English;fr:French' },
editable: true, edittype: 'select'

如果确实需要使用formatter: 'select',并且您需要从服务器通过Ajax加载editoptions.value,则必须设置editoptions.value 之前将处理从url返回的网格的主要数据。在这种情况下,我建议您扩展url返回的标准数据以及editoptions.value所需的数据。可以使用beforeProcessing回调(即使在您使用的复古版4.4.4中也支持)并使用editoptions.value方法动态设置setColProp。有关详细信息和代码示例,请参阅the answer

如果您不需要使用formatter: 'select' (如果ID和select中使用的值相同),那么您可以更改返回的数据格式从GetUnit操作到序列化数组:

["Unit1", "Unit2", "Unit2/3"]

并使用dataUrl buildSelect属性editoptions代替value dataUrlGetUnit的值应该是buildSelect action的URL,它返回所有utits的字符串数组。回调<select>应该将JSON数组转换为HTML片段,它代表width: "200px"所有选项。有关更多实现详细信息和代码示例,请参阅the old answer

最后,您应该将width: 200修复为widthpx属性的值应该是可以转换为数字的数字或字符串。使用index: 'UnitID'或其他后缀是错误的。如果index属性的值与colModel的值相同,则下一个建议的修复方法是从index删除name和所有其他SELECT phone_number FROM Calls WHERE System_outcome='No Answer' GROUP BY phone_number HAVING count(Phone_number) > 5; 属性属性。

相关问题