jquerystruts2jqgridstruts2-jquery-pluginstruts2-jquery-grid

struts 2 use jqGrid to get row data before format


I want to get the jGrid data after it has been loaded.

Some columns have formatter but I want to get their original data before format.

The grid columns are :

<sjg:gridColumn name="accountNo" formatter="linkBulider" />
<sjg:gridColumn name="amount" />

I use below js:

var allRowsInGrid = $('#gridtable').jqGrid('getRowData');
for (i = 0; i < allRowsInGrid.length; i++) {
   //For accountNo I get the formmated value 
   allRowsInGrid[i].accountNo;
   //The amount is ok as it is unformated
   allRowsInGrid[i].amount;
}

Solution

  • According to wiki getRowData doesn't return the actual data from the grid.

    Returns an array with data of the requested id = rowid. The returned array is of type name:value, where the name is a name from colModel and the value from the associated column in that row. It returns an empty array if the rowid can not be found.

    1. Do not use this method when you are editing the row or cell. This will return the cell content and not the actuall value of the input element.
    2. The performance of this method becomes an issue. Do not use this method in the body of “for” and “when”. (When calling this method, it will calculates the row datas one time.)

    If the rowid is not set the method return all the data from the grid in array

    Once you used a custom formatter option to format a cell content, you need an option to unformat the data.

    The question is: What to do if we use a custom formatter function and want to to have the original value back if we use editing or methods getRowData and getCell?

    The answer is: You can use your own custom unformatter function to do that. This function can be used in colModel.

    At this point you might check if the sjg:gridColumn could be set an attribute to define unformat function. Unfortunately, you can't do it according to TLD.

    However, you can modify the grid after it's loaded.

    $(document).ready(function(){
     $("#gridtable").jqGrid('setColProp', 'accountNo',{
        unformat: unformatFunc
      }).trigger("reloadGrid");
    });