0

This question may have been asked many times, but I would like to know if it possible to dynamically change the cell value of a jqgrid?

I basically have a grid that is that is being loaded with data via a JSon string. On some rows for a particular column that value may be "null". So knowing which row id upfront is one issue and then being able to change that "null" to something else. eg. change "null" to "Not Applicable".

All help would be appreciated. Thanks.

pundit
  • 772
  • 3
  • 18
  • 31
  • Which format has the JSON data? Do you use some `jsonReader` or you use [standard format](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data)? To be able to modify the JSON data one have to know the format of the input data. How will be sent `null`: as the `null` or as `"null"`? – Oleg Mar 15 '12 at 14:52

2 Answers2

3

I would recommend you to use beforeProcessing callback which is simple to use and which is very powerful. For example if you get the data from the server in the standard JSON format

{ 
    "total": "xxx", 
    "page": "yyy", 
    "records": "zzz",
    "rows" : [
        {"id": "1", "cell": ["cell11", "null", "cell13"]},
        {"id": "2", "cell": ["cell21", "cell22", null]},
        ...
    ]
}

you can do something like the following

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";
            }
        }
    }
}

In the way you can modify the data returned from the server before the data will be processed by the jqGrid.

Oleg
  • 220,925
  • 34
  • 403
  • 798
2

I don't think that changing the value is what you really need in this situation. You should rather create your own custom formatter. It might look more less like this:

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

This is just very basic, sample implementation but it should give you an idea.

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • Got it to work. Did the manipulation on the server side rather than the client side. thanks anyways... – pundit Mar 15 '12 at 14:58
  • 1
    @pundit: Why you spend the time of other people and ask your question if you have solution yourself **8 minutes** after you posted the question? – Oleg Mar 15 '12 at 15:17