【问题标题】:JQGRID show blank instead of NullJQGRID 显示空白而不是 Null
【发布时间】:2013-06-10 12:34:50
【问题描述】:

我正在使用JQGrid,并且由于它来自 DB,所以在网格中显示 Null。我可以更改查询以返回空白值。

但我尝试使用 JQGrid 来处理。我如何在网格中replace null by blank values

我不想向用户显示 NULL 而不是显示空白。

我如何在 JQGrid 中实现这一点?

谢谢

【问题讨论】:

    标签: jquery jqgrid


    【解决方案1】:

    最好由你处理这个服务器端,但如果你想在 jqGrid 中处理,你可以使用将 null 转换为空字符串的custom formatter。 (我不确定您是否真的取回了值null 或字符串"NULL",所以我处理了这两种情况):

    var nullFormatter = function(cellvalue, options, rowObject) {
        if(cellvalue === undefined || isNull(cellvalue) || cellvalue === 'NULL') {
            cellvalue = '';
        }
    
        return cellvalue;
    }
    
    $("#myGridContainer").jqGrid({
        ....
        colModel: [{
            label: 'Name',
            name:'name',
            index:'name',
            formatter:nullFormatter
        }, {
            label: 'Next Column',
            name:'nextCol',
            index:'nextCol',
            formatter: nullFormatter
        }, ...],
        ....
    }
    

    【讨论】:

    • nullundefined 应该自动转换 fiddle
    • 我确信 服务器代码 返回 "null""NULL" 而不是 null 对应 JSON 格式。所以服务器代码有错误。使用 SQL 的 [ISNULL][1] 函数修复服务器代码或其他代码修复将是最好的解决方案。如果使用自定义格式化程序,则 aotoencode: true 选项将不适用于该列。我建议在 nullFormatter 格式化程序中包含对 $.jgrid.htmlEncode 的显式调用并定义 unformat 函数。
    【解决方案2】:

    我遇到了同样的问题。

    另外,我希望jqGrid 以千位分隔符和 2 个小数位显示我的数字,但使用默认的 'number' 格式化程序导致任何 nulls(来自数据库)显示为“ 0.00" 而不是留空。

    $("#tblListOfRecords").jqGrid({
        ...
        colModel: [
          { name: "SomeNumber", formatter: 'number', sorttype: "integer", formatoptions: { decimalPlaces: 2 } }
        ]
        ...
    

    这不是我想要的。

    我的解决方案是编写自己的格式化程序:

    $("#tblListOfRecords").jqGrid({
        ...
        colModel: [
          { name: "SomeNumber", formatter: formatNumber}
        ]
    });
    
    function formatNumber(cellValue, options, rowdata, action) {
        //  Convert a jqGrid number string (eg "1234567.89012") into a thousands-formatted string "1,234,567.89" with 2 decimal places
        if (cellValue == "")
            return "";
        if (cellValue == null || cellValue == 'null')
            return "";
    
        var number = parseFloat(cellValue).toFixed(2);          //  Give us our number to 2 decimal places
        return number.toLocaleString();                         //  "toLocaleString" adds commas for thousand-separators.
    }
    

    【讨论】:

    • cellValue 会导致问题,应该是 cellvalue...否则这对我有用!
    猜你喜欢
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多