【问题标题】:jqGrid error when data is null数据为空时的jqGrid错误
【发布时间】: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 == nullcellvalue === 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


【解决方案1】:

如果您使用var nullformatter = function (cellvalue, options, rowobject) {...} 语法,那么您必须移动nullformatter 的定义 使用之前(在您使用$('#list').jqGrid({...}); 创建网格之前)。您当前的代码使用formatter: undefined。只有你会使用function nullformatter (cellvalue, options, rowobject) {...},你才能定义下面的用法。

【讨论】:

  • @mahdisdezfouli:看看演示jsfiddle.net/OlegKi/u9x2fcz1/6,它使用了您的代码和一些测试数据。它正确加载数据。我看不出有什么问题。您应该将其与您的代码和从服务器返回的数据进行比较。 (演示使用 JSFiddle 回显服务,它允许模拟来自服务器的 JSON 响应)
  • 对不起这个错误可能是 NULL 。因为在db中,有NULL。并且在演示中,当我更改(null 到 NULL)时,这不会显示。
  • @mahdisdezfouli:不客气!我建议您使用Fiddler 进行 HTTP 跟踪或至少使用 IE/Chrome/Firefox 的开发者工具(按 F12 开始)。如果您将看到服务器和 jqGrid 之间的完整通信,您肯定会解决问题,或者至少在我发布给您的演示 jsfiddle.net/OlegKi/u9x2fcz1/6 中包含服务器的 JSON。我个人非常频繁地使用 Fiddler。
  • @mahdisdezfouli:不客气!请在您找到原因或从重现问题的服务器收到 JSON 响应后通知您。
  • 对不起。当我使用演示:jsfiddle.net/OlegKi/u9x2fcz1/6 并更改(null 为 NULL)它不起作用。当我使用资本时,它是错误的。为什么?请帮帮我。
【解决方案2】:

您可以添加以下条件吗?它对我有用。

var nullformatter = function (cellvalue, options, rowobject) {
            if (cellvalue == undefined || isnull(cellvalue) || cellvalue == 'NULL' || cellvalue.trim().length==0)
            { 
              cellvalue = ' '; 
            } 
          return cellvalue;
        }

如果上述条件不起作用,则提醒单元格值。

alert('cell value==>>'+cellvalue);

之后你会更正它。

或尝试遵循条件

if(null!=cellvalue && cellvalue.length>0){
   return cellvalue;
}else{
        return " ";
 }

【讨论】:

  • 我在哪里使用警报?
  • 在 if 语句之前...假设它的显示对象...使用跟随警报(JSON.stringify(cellvalue));
  • alert('cell value==>>'+JSON.stringify(cellvalue));
  • 不显示警报。 alert('单元格值==>>'+JSON.stringify(cellvalue));
  • 我在 if 语句之前使用 alert 但不显示 alert
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多