我正在使用 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>