jqGrid默认值不起作用?

我有一个jqGrid,添加新行时,一列应该有一个默认值。 我已经按照文档,但价值永远不会出现。

这是colModel中的列:

{name: 'Lt', index: 'Lt', width: 25, editable: true, editoptions: {defaultValue: 'N'}} 

这就是我调用addRowData的方式:

 $("#grid").addRowData(-1, { Datum: $.datepicker.formatDate("yy-mm-dd", new Date()) }, "first", true) 

据我所见,我正在书中做到这一点!

完成网格定义:

  $("#dagbok_grid").jqGrid({ datatype: 'json', mtype: 'GET', colNames: [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's'], colModel: [ { name: 'a', index: 'a', width: 30, formatter: 'checkbox', edittype: 'checkbox', editable: true }, { name: 'b', index: 'b', width: 30, formatter: 'checkbox', edittype: 'checkbox', editable: true }, { name: 'c', index: 'c', width: 70, formatter: 'date', editable: true, editrules: { required: true }, editoptions: { dataInit: function (elem) { $(elem).datepicker(); } } }, { name: 'd', index: 'd', width: 65, editable: true, formatter: 'date', formatoptions: { srcformat: 'H:i:s', newformat: 'ShortTime' }, editrules: { time: true} }, { name: 'e', index: 'e', width: 80, edittype: 'select', editable: true, formatter: 'select' }, { name: 'f', index: 'f', width: 100, editable: true, edittype: 'textarea', editoptions: {rows: '7'} }, { name: 'g', index: 'g', width: 80, editable: true, edittype: 'textarea', editoptions: { rows: '7'} }, { name: 'h', index: 'h', width: 80, editable: true, editrules: { maxValue: 50} }, { name: 'i', index: 'i', width: 120, edittype: 'select', editable: true, formatter: 'select' }, { name: 'j', index: 'j', width: 200, edittype: 'select', editable: true, formatter: 'select' }, { name: 'k', index: 'k', width: 70, edittype: 'select', editable: true, formatter: 'select' }, { name: 'l', index: 'l', width: 70, editable: true, editrules: { maxValue: 10} }, { name: 'm', index: 'm', width: 25, editable: true, editoptions: { defaultValue: 'N'} }, { name: 'n', index: 'n', width: 70, editable: true, editrules: { integer: true, maxValue: 999999 }, formatter: formatPosition, unformat: unformatPosition }, { name: 'o', index: 'o', width: 25, editable: true, editrules: { custom: true, custom_func: chkLongitudTecken} }, { name: 'p', index: 'p', width: 80, editable: true, editrules: { integer: true, maxValue: 999999 }, formatter: formatPosition, unformat: unformatPosition }, { name: 'q', index: 'q', width: 80, edittype: 'select', editable: true, formatter: 'select' }, { name: 'r', index: 'r', width: 100, editable: true, editrules: { maxValue: 50} }, { name: 's', index: 's', width: 65, edittype: 'select', editable: true, formatter: 'select' }, ], sortname: 'c', sortorder: 'desc', shrinkToFit: false, viewrecords: true, gridview: true, onSelectRow: function (id) { if (id && id !== lastsel) { jQuery('#dagbok_grid').saveRow(lastsel); lastsel = id; } jQuery('#dagbok_grid').editRow(id, true); }, height: 400, editurl: '@Url.Action("SaveGridRow")', caption: 'my grid' }); 

我稍后也会在某些cols中添加select-values:

  $.ajax({ type: 'GET', url: '@Url.Action("GetGridComboValues")', async: false, success: function (data) { var grid = $("#dagbok_grid"); grid.setColProp("e", { editoptions: { value: data.kallor} }); grid.setColProp("i", { editoptions: { value: data.rubriker} }); grid.setColProp("j", { editoptions: { value: data.verksamheter} }); grid.setColProp("k", { editoptions: { value: data.opPadrag, dataEvents: [{ type: 'change', fn: function (e) { $.ajax({ type: "GET", url: '@Url.Action("GetVerksamhetskod")', data: { "opPadragId": e.currentTarget.value }, dataType: 'json', success: function (data) { var selr = jQuery('#dagbok_grid').jqGrid('getGridParam', 'selrow'); jQuery("#dagbok_grid").jqGrid('setCell', selr, "l", data); } }); } }] } }); grid.setColProp("q", { editoptions: { value: data.medier} }); grid.setColProp("s", { editoptions: { value: data.regioner} }); } }); 

添加和删​​除代码

  $("#toolbarAddButton") .button() .click(function () { $("#dagbok_grid").addRowData(-1, { Datum: $.datepicker.formatDate("yy-mm-dd", new Date()) }, "first", true) }); $("#toolbarDeleteButton").click(function () { var radid = $("#dagbok_grid").jqGrid("getGridParam", "selrow"); if (radid != null) { var su = $("#dagbok_grid").jqGrid("delRowData", radid); $("#dagbok_grid").jqGrid("delGridRow", radid, { url: '@Url.Action("DeleteGridRow")', reloadaftersubmit: false }); } }) 

当用户在编辑模式下按进入时执行保存。

首先,您定义了editoptions: { defaultValue: 'N'} 。 您可以阅读editoptions的文档

选项可以是字符串或函数。 当在添加模式下与editGridRow方法一起使用时,此选项仅在表单编辑模块中有效。 如果已定义,则只有元素为空时才使用此值设置input元素。 如果用于选择,则应提供文本而不是密钥。 此外,当使用函数时,函数应返回值。

你想要的是在填充网格体期间而不是在编辑期间设置默认值。 出于此目的存在defaulValue formatter选项,但它仅用于某些预定义的格式化程序(请参阅文档 )。

您可以通过使用自定义格式化程序来解决问题,或者,如果直接使用addRowData方法,则可以通过显式添加Lt列的值来解决问题。

我不建议使用低级方法addRowData ,这种方法很慢并且主要以错误的方式使用。 您使用例如-1作为rowid。 如果您将执行相同的语句更多次,您将有id重复,这是错误,可以跟随非常奇怪的效果。 如果你想让jqGrid生成唯一的id本身,你应该使用undefined作为rowid参数。