有没有办法动态更改jqGrid的单元格值?

时间:2012-03-15 13:53:15

标签: javascript jquery jqgrid

这个问题可能已被多次询问,但我想知道是否可以动态更改jqgrid的单元格值?

我基本上有一个通过JSon字符串加载数据的网格。在特定列的某些行上,该值可能为“null”。因此,事先了解哪个行ID是一个问题,然后能够将“null”更改为其他内容。例如。将“null”更改为“Not Applicable”。

所有帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:3)

我建议您使用beforeProcessing回调,它易于使用且功能非常强大。例如,如果您从the standard JSON format

中获取服务器中的数据
{ 
    "total": "xxx", 
    "page": "yyy", 
    "records": "zzz",
    "rows" : [
        {"id": "1", "cell": ["cell11", "null", "cell13"]},
        {"id": "2", "cell": ["cell21", "cell22", null]},
        ...
    ]
}

您可以执行以下操作

beforeProcessing: function (data) {
    var rows = data.rows, cRows = rows.length, row, iRow, cCol, iCol, cell;
    for (iRow = 0; iRow < cRows; iRow++) {
        row = rows[iRow].cell;
        for (iCol = 0, cCol = row.length; iCol < cCol; iCol++) {
            cell = row[iCol];
            if (cell === null || cell === "null") {
                row[iCol] = "Not Applicable";
            }
        }
    }
}

在之前,您可以修改从服务器返回的数据,jqGrid将处理数据。

答案 1 :(得分:2)

我不认为在这种情况下你真正需要改变价值。您应该创建自己的custom formatter。它可能看起来更像是这样:

var nullFormatter = function(cellvalue, options, rowObject) {
    if (cellValue == null)
        return "Not Applicable";
    else
        return cellValue;
};

这只是非常基本的示例实现,但它应该给你一个想法。