【发布时间】:2015-07-06 23:24:08
【问题描述】:
我正在使用 jqGrid 来显示数据,其中一些字段在数据库中可能为空。出现空值时,我得到错误,对象引用未设置为对象的实例。
我已添加代码将 null 更改为字符串,如下所示,但 jqGrid 未显示:
<script type="text/javascript">
$(document).ready(function () {
$('#list').jqGrid({
caption: "MM",
url: '@Url.Action("GetAll", "Grid")',
datatype: 'json',
jsonReader: {
root: "Rows",
page: "Page",
total: "Total",
records: "Records",
repeatitems: true,
id: "Id",
cell: "RowCells"
},
mtype: 'POST',
colNames: ['Serial'],
colModel: [
{
name: 'Serial', index: 'Serial', align: 'center', width: 60, formatter: nullformatter
}
],
pager: $('#pager'),
rowNum: 10,
rowList: [10, 20, 50, 100],
sortname: 'Id',
sortorder: 'desc',
viewrecords: true,
altRows: true,
shrinkToFit: false,
width: '1041',
height: 'auto',
hidegrid: false,
direction: "rtl",
gridview: true,
rownumbers: true,
footerrow: true,
loadComplete: function () {
$("tr.jqgrow:odd").css("background", "#E0E0E0");
},
loadError: function (xhr, st, err) {
jQuery("#rsperror").html("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText);
}
})
var nullformatter = function (cellvalue, options, rowobject) {
alert('cell value==>>' + cellvalue);
if (cellvalue == undefined || isnull(cellvalue) || cellvalue == 'NULL' || cellvalue.trim().length == 0) {
cellvalue = ' ';
}
return cellvalue;
};
})
</script>
【问题讨论】:
-
可能是
isnull函数的使用?没有标准的 JavaScript 函数isnull。你是否以某种方式定义了它?您应该尝试删除部分|| isnull(cellvalue)或只是将主体替换为return cellvalue == null || cellvalue === "NULL" ? "" : cellvalue;测试cellvalue == null与cellvalue === null || cellvalue === undefined相同。 -
@Oleg 我用过但没用
-
@Oleg 我的函数不被格式化程序调用。
-
你介意
nullformatter永远不会被调用吗?可能您在错误的范围内定义了nullformatter?您是否在调试器中启动代码?您只需按 F12 启动开发人员工具,然后开始调试,出错时您将看到错误“对象引用未设置为对象实例”的确切位置。 -
你可以尝试使用匿名函数
{name: 'Version',..., formatter: function (cellvalue) { return cellvalue == null || cellvalue === "NULL" ? "" : cellvalue; }}
标签: jquery model-view-controller jqgrid jqgrid-formatter mvcjqgrid