0

Answer in

How to replace remapColums with remapColumnsByName in free jqgrid

contains code to save and restore jqgrid column order.

It contains method to restore columns state:

var restoreColumnState = function (colModel) {
  var colItem, i, l = colModel.length, colStates, cmName,
    columnsState = getObjectFromLocalStorage(myColumnStateName);

if (columnsState) {
    colStates = columnsState.colStates;
    for (i = 0; i < l; i++) {
        colItem = colModel[i];
        cmName = colItem.name;
        if (cmName !== "rn" && cmName !== "cb" && cmName !== "subgrid") {
                colModel[i] = $.extend(true, {}, colModel[i], colStates[cmName]);
        }
    }
}
return columnsState;

};

This method causes invalid data posting from inline edit if new column is defined in server side.

jqgrid is populated from remote json data array. In this array columns must be the same as in column state. If columns state is saved and new column is added to jqgrid in server code, colStates[cmName] value is undefined. This code causes new column to be added to end of jqgrid columns. However, in json data array it appears in the column as defined in server. On inline edit, if row is saved, wrong values are assigned to form fields and invalid values are passed to server.

I tried to fix it adding colStates[cmName] !== undefined check:

        if (cmName !== "rn" && cmName !== "cb" && cmName !== "subgrid" && colStates[cmName] !== undefined) {

but problem persists. How to fix this that if new column is added to jqgrid colmodel in server, restoring column state allows to save correct data?

New column which is not found in saved columns list should appear in the same relative position as it is defined in colmodel. Column order shoudl corrspond to remote data from server.

Update

ColModel is defined in Razor view in variable cm

     <script>
    var
        $grid,
          myColumnsState,
          isColState,
          myColumnStateName;

        $(function () {
            var cm= @Html.Raw(Model.GetColModel());
            $grid = $("#grid");
            myColumnStateName = @Model.ColumnStateName();
            myColumnsState = restoreColumnState(cm, myColumnStateName);
            isColState = typeof (myColumnsState) !== 'undefined' && myColumnsState !== null;
            $grid.jqGrid({
                page: isColState ? myColumnsState.page : 1,
                sortname: isColState ? myColumnsState.sortname : "",
                sortorder: isColState ? myColumnsState.sortorder : "",
        ....
                </script>
Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

0

I know the problem very good! One need to implement some kind of validating checks of the previously saved state of the grid before the usage. The deepness of checks could depend on the exact requirements of your application and from the information which one knows exactly. The most opened and unclear thing: should one make some correction/fixing of the previously saved state or should one discard the state on the first small error? The answer on the question depends on the project where jqGrid are used. Deep fixing could include fixing of sorting parameter and modifying previously saved filter. Another example: the state could include ids of selected rows, but the fixing of the part of the state could be bad idea in the common case. One loading of the data could imply one setting of selected rows, but loading of another data (unfiltered for example) could do have the rows and the rows should be do selected. There are no best choice in the case, all depends on the exact project requirements. In any way the implementation of the state validation/fixing isn't a simple code.

Only because of the complexity of the problems of validation of previously saved state and the existence of different scenarios of validation I didn't implemented such feature in free jqGrid. Any good implementation needs time and the resulting code will be not simple. It will have some options for some typical scenarios. I would like to implement the feature in the future, but I just didn't found the time for the implementation, because I have to do my main job to earn money for my family and I still try to help other people in the community who have small, but important, for the person, problems with jqGrid of free jqGrid.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • How to add something like checksum to jqrid colmodel ? If colmodel is changed in any way compared to saved state, state should not restored. Or maybe to save whole colmodel defintion and compare with current definition in restore. If there is any difference, state should not restored. – Andrus Nov 25 '15 at 21:27
  • @Andrus: If you make changed in you model you have to modify two places of the code: backend (controller action for example) and to change `colModel`. Even if you load the `colModel` from the server you can include the validation of the saved state of the grid *after* the `colModel` is successfully loaded, but *before* jqGrid is created. – Oleg Nov 25 '15 at 21:36
  • I updated question and added colmodel definition in view. How to add saved state validation ? Colmodel is json object assigned to cm variable. Maybe it is possible to calculate md5 digest from it and save it for easy validation – Andrus Nov 26 '15 at 07:34
  • @Andrus: One can add validation of `myColumnsState` directly before the line `$grid.jqGrid({...`. You have here `cm`, `myColumnsState` and all information what you need. – Oleg Nov 26 '15 at 07:39
  • How to compare `cm` and `myColumnsState` objects ? Is there some jquery method which can compare only properties which exist in saved column state ? Or is it better from server to return last change timestamp or md5 digest of columnstate ? This can stored in localStorage and compared with new colmodel defore restore – Andrus Nov 26 '15 at 15:19