我有一个 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'
});
稍后我还将选择值添加到一些列:
$.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 });
}
})
当用户在编辑模式下按 enter 时执行保存。